Python – Convert dictionary items to values

Sometimes, while working with Python dictionary, we can have a problem in which we need to convert all the items of dictionary to a separate value dictionary. This problem can occur in applications in which we receive dictionary in which both keys and values need to be mapped as separate values. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘Gfg’: 1}
Output : [{‘key’: ‘Gfg’, ‘value’: 1}]

Input : test_dict = {‘Gfg’: 1, ‘best’: 5}
Output : [{‘key’: ‘Gfg’, ‘value’: 1}, {‘key’: ‘best’, ‘value’: 5}]

Method #1 : Using loop
This is brute force way to solve this problem. In this, we need to run a loop to assign each item of dictionary a different value.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert dictionary items to values
# Using loop
  
# initializing dictionary
test_dict = {'Gfg': 1, 'is': 2, 'best': 3}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# Convert dictionary items to values
# Using loop
res = []
for key, val in test_dict.items():
    res.append({"key": key, "value": val})
         
# printing result 
print("Converted Dictionary : " + str(res)) 

chevron_right


Output :



The original dictionary : {‘Gfg’: 1, ‘is’: 2, ‘best’: 3}
Converted Dictionary : [{‘key’: ‘Gfg’, ‘value’: 1}, {‘key’: ‘is’, ‘value’: 2}, {‘key’: ‘best’, ‘value’: 3}]

 

Method #2 : Using list comprehension
This is yet another way in which this task can be performed. This solves problem in similar way as above, but it a more compact way in form of shorthand.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Convert dictionary items to values
# Using list comprehension
  
# initializing dictionary
test_dict = {'Gfg': 1, 'is': 2, 'best': 3}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# Convert dictionary items to values
# Using list comprehension
res = [{'key': key, 'value': test_dict[key]} for key in test_dict]
         
# printing result 
print("Converted Dictionary : " + str(res)) 

chevron_right


Output :

The original dictionary : {‘Gfg’: 1, ‘is’: 2, ‘best’: 3}
Converted Dictionary : [{‘key’: ‘Gfg’, ‘value’: 1}, {‘key’: ‘is’, ‘value’: 2}, {‘key’: ‘best’, ‘value’: 3}]




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.