Open In App

Python | Extract key-value of dictionary in variables

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

Time Complexity: O(n)
Auxiliary Space: O(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)

Method #4: Using a for loop to iterate over dictionary

Step-by-step approach:

  • Initialize dictionary
  • Create two empty variables, key and val
  • Use a for loop to iterate over the dictionary and assign the key-value pairs to the variables key and val respectively
  • Print the result

Python




# Initialize dictionary
test_dict = {'gfg': 1}
 
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
 
# Using for loop
# Extracting key-value of dictionary in variables
key, val = None, None
for k, v in test_dict.items():
    key, val = k, v
    break
 
# 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) where n is the number of key-value pairs in the dictionary
Auxiliary Space: O(1)

Method 5 : using list conversion.

we initializes a dictionary with one key-value pair and prints the original dictionary. Then, it uses the list conversion method to extract the first key-value pair of the dictionary and assigns them to the variables ‘key’ and ‘val’. Finally, it prints the extracted key and value. 

Python3




# Initialize dictionary
test_dict = {'gfg': 1}
 
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
 
# Using list conversion
# Extracting key-value of dictionary in variables
key, val = list(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

The time complexity for all the methods mentioned above is O(1), as we are only accessing the first element of the dictionary, which takes constant time. 

The space complexity is also constant, as we are only using a constant amount of memory to store the key-value pair.



Last Updated : 26 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads