Python | Extract key-value of dictionary in variables
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 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)) |
The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1
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 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)) |
The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1
Recommended Posts:
- Python | Extract specific keys from dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- numpy.extract() in Python
- Python | Extract numbers from string
- Python | Extract only characters from given string
- Python | Extract words from given string
- Python | Extract URL from HTML using lxml
- Python | Pandas Series.str.extract()
- Python | Extract digits from given string
- Extract images from video in Python
- Python | Extract Combination Mapping in two lists
- Python | Program to extract frames using OpenCV
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



