Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Convert dictionary items to values

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. 

Python3




# 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))


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




# 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))


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




# Python3 code to demonstrate working of
# Convert dictionary items to values
# Using dict.items() and 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 dict.items() and loop
res = []
for key, value in test_dict.items():
    temp_dict = {'key': key, 'value': value}
    res.append(temp_dict)
 
# printing result
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments