Python – Extract Dictionary values list to List
Sometimes, while working with dictionary records, we can have problems in which we need to extract all the dictionary values into a single separate list. This can have possible application in data domains and web-development. Lets discuss certain ways in which this task can be performed.
Method #1 : Using map() + generator expression The combination of above functionalities can be used to solve this problem. In this, we perform the task of extraction of values using generator expression and map() is used to reconstruct the value lists.
Python3
test_dict = { 'gfg' : [ 4 , 6 , 7 , 8 ],
'is' : [ 3 , 8 , 4 , 2 , 1 ],
'best' : [ 9 , 5 , 2 , 1 , 0 ]}
print ("The original dictionary is : " + str (test_dict))
res = list ( map ( list , (ele for ele in test_dict.values())))
print ("The list of dictionary values : " + str (res))
|
Output :
The original dictionary is : {‘is’: [3, 8, 4, 2, 1], ‘gfg’: [4, 6, 7, 8], ‘best’: [9, 5, 2, 1, 0]} The list of dictionary values : [[3, 8, 4, 2, 1], [4, 6, 7, 8], [9, 5, 2, 1, 0]]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using map() The combination of above functions can be used to solve this problem. In this we perform the task of extraction and remaking using map().
Python3
test_dict = { 'gfg' : [ 4 , 6 , 7 , 8 ],
'is' : [ 3 , 8 , 4 , 2 , 1 ],
'best' : [ 9 , 5 , 2 , 1 , 0 ]}
print ("The original dictionary is : " + str (test_dict))
res = list ( map ( list , (test_dict.values())))
print ("The list of dictionary values : " + str (res))
|
Output :
The original dictionary is : {‘is’: [3, 8, 4, 2, 1], ‘gfg’: [4, 6, 7, 8], ‘best’: [9, 5, 2, 1, 0]} The list of dictionary values : [[3, 8, 4, 2, 1], [4, 6, 7, 8], [9, 5, 2, 1, 0]]
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 3: Using a list comprehension
use a list comprehension to iterate over the keys of the dictionary and extract the corresponding values. The resulting list is stored in the variable res.
- Initialize a dictionary test_dict with key-value pairs.
- Create an empty list res to store the dictionary values.
- Use a loop to iterate over the values of the dictionary using the values() method.
- For each value, append it to the res list.
- Return the res list.
Python3
test_dict = { 'gfg' : [ 4 , 6 , 7 , 8 ], 'is' : [ 3 , 8 , 4 , 2 , 1 ], 'best' : [ 9 , 5 , 2 , 1 , 0 ]}
res = [test_dict[key] for key in test_dict]
print ( "The list of dictionary values : " + str (res))
|
Output
The list of dictionary values : [[4, 6, 7, 8], [3, 8, 4, 2, 1], [9, 5, 2, 1, 0]]
Time complexity: O(n), where n is the total number of values in the dictionary.
Auxiliary space: O(n), where n is the total number of values in the dictionary, since we create a new list to store the values.
Method 4 : using a loop to iterate over the values of the dictionary and append them to a new list.
step-by-step breakdown of the program:
First, we initialize a dictionary named test_dict with three keys and their corresponding values.
We then print the original dictionary using the print() function and concatenation with the str() function to convert the dictionary to a string.
Next, we create an empty list called res.
We then use a for loop to iterate over the values of the test_dict dictionary using the values() method.
Inside the loop, we append each value to the res list using the append() method.
After the loop has finished, we print the list of dictionary values using the print() function and concatenation with the str() function to convert the list to a string.
Python3
test_dict = { 'gfg' : [ 4 , 6 , 7 , 8 ],
'is' : [ 3 , 8 , 4 , 2 , 1 ],
'best' : [ 9 , 5 , 2 , 1 , 0 ]}
print ( "The original dictionary is: " + str (test_dict))
res = []
for value in test_dict.values():
res.append(value)
print ( "The list of dictionary values: " + str (res))
|
Output
The original dictionary is: {'gfg': [4, 6, 7, 8], 'is': [3, 8, 4, 2, 1], 'best': [9, 5, 2, 1, 0]}
The list of dictionary values: [[4, 6, 7, 8], [3, 8, 4, 2, 1], [9, 5, 2, 1, 0]]
The time complexity of this approach is O(n), where n is the total number of values in the dictionary.
The auxiliary space used is also O(n), as we are creating a new list to store the values.
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...