Python – Convert dictionary items to values
Last Updated :
25 Apr, 2023
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.
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ("The original dictionary : " + str (test_dict))
res = []
for key, val in test_dict.items():
res.append({"key": key, "value": val})
print ("Converted Dictionary : " + str (res))
|
Output :
The original dictionary : {‘Gfg’: 1, ‘is’: 2, ‘best’: 3} Converted Dictionary : [{‘key’: ‘Gfg’, ‘value’: 1}, {‘key’: ‘is’, ‘value’: 2}, {‘key’: ‘best’, ‘value’: 3}]
Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
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.
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ("The original dictionary : " + str (test_dict))
res = [{ 'key' : key, 'value' : test_dict[key]} for key in test_dict]
print ("Converted Dictionary : " + str (res))
|
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 3: using the built-in dict.items() method along with a for loop.
- Initialize a dictionary test_dict with key-value pairs.
- Initialize an empty list res.
- Loop through the items of the dictionary using the items() method.
- For each item, create a dictionary with key ‘key’ and value as the key of the item, and key ‘value’ with value as the value of the item.
- Append the dictionary created in step 4 to the res list.
- Print the original dictionary and the converted dictionary.
Python3
test_dict = { 'Gfg' : 1 , 'is' : 2 , 'best' : 3 }
print ( "The original dictionary : " + str (test_dict))
res = []
for key, value in test_dict.items():
temp_dict = { 'key' : key, 'value' : value}
res.append(temp_dict)
print ( "Converted Dictionary : " + str (res))
|
Output
The original dictionary : {'Gfg': 1, 'is': 2, 'best': 3}
Converted Dictionary : [{'key': 'Gfg', 'value': 1}, {'key': 'is', 'value': 2}, {'key': 'best', 'value': 3}]
Time complexity: O(n), where n is the number of items in the dictionary. :
Auxiliary space: O(n), where n is the number of items in the dictionary, because we are creating a new list with n items.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...