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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extracting Dictionary values list to List
# Using map() + generator expression
  
# initializing dictionary
test_dict = {'gfg' : [4, 6, 7, 8],
             'is' : [3, 8, 4, 2, 1],
             'best' : [9, 5, 2, 1, 0]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Extracting Dictionary values to List
# Using map() + generator expression
res = list(map(list, (ele for ele in test_dict.values())))
      
# printing result 
print("The list of dictionary values : " + str(res)) 

chevron_right


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]]

 



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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extracting Dictionary values list to List
# Using map()
  
# initializing dictionary
test_dict = {'gfg' : [4, 6, 7, 8],
             'is' : [3, 8, 4, 2, 1],
             'best' : [9, 5, 2, 1, 0]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# Extracting Dictionary values to List
# Using map()
res = list(map(list, (test_dict.values())))
      
# printing result 
print("The list of dictionary values : " + str(res)) 

chevron_right


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]]




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.