Categories
Python Answers

How to get list of values from a Python dict?

Spread the love

Sometimes, we want to get list of values from a Python dict.

In this article, we’ll look at how to get list of values from a Python dict.

How to get list of values from a Python dict?

To get list of values from a Python dict, we can use the dict’s values method and the list function.

For instance, we write:

d = {'a': 1, 'b': 2}
v = list(d.values())
print(v)

We call d.values to return a view of the values.

Then we use the list function to convert the values view into a list.

Therefore, v is [1, 2].

Conclusion

To get list of values from a Python dict, we can use the dict’s values method and the list function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *