Home >Backend Development >Python Tutorial >How to Get a List of Dictionary Values in Python?

How to Get a List of Dictionary Values in Python?

Linda Hamilton
Linda HamiltonOriginal
2024-11-26 01:56:11994browse

How to Get a List of Dictionary Values in Python?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn