Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Extract key-value of dictionary in variables

Improve Article
Save Article
  • Last Updated : 30 Jan, 2023
Improve Article
Save Article

Sometimes, while working with dictionaries, we can face a problem in which we may have just a singleton dictionary, i.e dictionary with just a single key-value pair, and require to get the pair in separate variables. This kind of problem can come in day-day programming. Let’s discuss certain ways in which this can be done. 

Method #1: Using items() This problem can be solved using the items function which does the task of extracting a key-value pair and using the 0th index gives us the first key-value pair. Works only in Python2. 

Python




# Python code to demonstrate working of
# Extracting key-value of dictionary in variables
# Using items()
 
# Initialize dictionary
test_dict = {'gfg' : 1}
 
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
 
# Using items()
# Extracting key-value of dictionary in variables
key, val = test_dict.items()[0]
 
# printing result
print("The 1st key of dictionary is : " + str(key))
print("The 1st value of dictionary is : " + str(val))

Output : 

The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1

Time complexity: O(1), as the operation of extracting the key value of a dictionary using items() is constant time, i.e., it takes O(1) time to extract the first item in the dictionary.
Auxiliary space: O(1), as it only requires a constant amount of memory to store the variables key and val and the operation of extracting the first item from the dictionary does not create any additional memory overhead.

Method #2: Using iter() + next() The combination of above functions can be used to perform this particular task. It uses iterators to perform this task. The next() is used to fetch the pairs till dictionary is exhausted. It works with Python3. 

Python3




# Python3 code to demonstrate working of
# Extracting key-value of dictionary in variables
# Using iter() + next()
 
# Initialize dictionary
test_dict = {'gfg' : 1}
 
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
 
# Using iter() + next()
# Extracting key-value of dictionary in variables
key, val = next(iter(test_dict.items()))
 
# printing result
print("The 1st key of dictionary is : " + str(key))
print("The 1st value of dictionary is : " + str(val))

Output : 

The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1

Method #3 : Using keys(),values()

Python3




# Python code to demonstrate working of
# Extracting key-value of dictionary in variables
 
# Initialize dictionary
test_dict = {'gfg' : 1}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
 
# Extracting key-value of dictionary in variables
key=list(test_dict.keys())[0]
val=list(test_dict.values())[0]
 
# printing result
print("The 1st key of dictionary is : " + str(key))
print("The 1st value of dictionary is : " + str(val))

Output

The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!