How to get Dictionary value as list Python(4 ways)

In this post, we will learn How to get Dictionary values as a list in Python with examples. The dictionary is a built-in datatype in Python that stores data in key-value pairs. The dictionary keys are always unique,but values can be duplicate. The values are stored in a dictionary based on the key and the keys are passed to extract the value.

1. How to Dictionary value as list Python using dict.values()


In this example we have used dict.values() method that returns a view object of list of values in dictionary.The list() method is used to convert view object into a list of values.

 
dict_sub = {'math':100,'Eng':100,'Chem':98,'students':['jack','mack']}
 
list_Values = list(dict_sub.values())
 
print('dict values as list:',list_Values)

Output

dict key as list of values: [100, 100, 98, ['jack', 'mack']]

2. How to get Dictionary value as list Python using list.extend()


We have defined an empty list and used list.extend() method to combine two lists, add all elements of a list to the end of another list.The dict.values() method is used as a parameter to list.extend() to append dictionary values to a list.

 
dict_sub = {'math':100,'Eng':100,'Chem':98,'students':['jack','mack']}
my_list = []
my_list.extend(dict_sub.values())
 
print('dict values as list:',my_list)

Output

dict key as list of values: [100, 100, 98, ['jack', 'mack']]

3. Get Dictionary value as list Python using * operator


In this example ,the asterik operator(*) is upack operator that unpacks values of a given iterable to get dictionary values as list Python.Let us understand with below program.

 
dict_sub = {'math':100,'Eng':100,'Chem':98,'students':['jack','mack']}

 
print('dict values as list:',[*dict_sub.values()])

Output

dict values as list: [100, 100, 98, ['jack', 'mack']]

4. How to Dictionary value as list using list comprehension


In this example,We have used list comprehension iterating over the dictionary key-value pair and to get the dictionary values as list.

 
dict_sub = {'math':100,'Eng':100,'Chem':98,'students':['jack','mack']}


res_list = [dict_sub[key] for key in dict_sub]
 
print('dict values as list:',res_list)

Output

dict values as list: [100, 100, 98, ['jack', 'mack']]