How to get a list of all the values from a Python dictionary?

PythonServer Side ProgrammingProgramming

To get a list of all keys from a dictionary, you can simply use the dict.values() function. 

Example

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'}
value_list = list(my_dict.values())
print(value_list)

Output

This will give the output −

['TutorialsPoint', '15 years', 'India']

You can also get a list of all values in a dictionary using a list comprehension. 

Example

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'}
value_list = [my_dict[key] for key in my_dict]
print(value_list)

Output

This will give the output −

['TutorialsPoint', '15 years', 'India']
raja
Published on 05-Jan-2018 13:12:42
Advertisements