Obtaining a List of Dictionary Values in Python
Python's dictionaries, analogous to Java's Maps, provide a means of storing key-value pairs. To facilitate accessing the values within a dictionary as a list, Python offers a straightforward solution:
Solution:
Utilize the dict.values() method, which yields a view of the dictionary's values. To convert this view into a list, simply wrap it in the list function:
list_of_values = list(d.values())
Example:
Consider the following dictionary:
d = {'name': 'John', 'age': 30, 'city': 'New York'}
To obtain a list of the values, we use the following code:
values_list = list(d.values()) print(values_list)
Output:
['John', 30, 'New York']
The above is the detailed content of How to Get a List of Dictionary Values in Python?. For more information, please follow other related articles on the PHP Chinese website!