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
Release: 2024-11-26 01:56:11
Original
993 people have browsed it

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())
Copy after login

Example:

Consider the following dictionary:

d = {'name': 'John', 'age': 30, 'city': 'New York'}
Copy after login

To obtain a list of the values, we use the following code:

values_list = list(d.values())
print(values_list)
Copy after login

Output:

['John', 30, 'New York']
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template