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



