Categories
Python Answers

How to get list of values for list of keys with Python dictionaries?

Spread the love

Sometimes, we want to get list of values for list of keys with Python dictionaries.

In this article, we’ll look at how to get list of values for list of keys with Python dictionaries.

How to get list of values for list of keys with Python dictionaries?

To get list of values for list of keys with Python dictionaries, we can use list comprehension.

For instance, we write

l = [mydict[x] for x in mykeys]

to get the keys from mykeys and get the values of each key x in the mydict dict with mydict[x].

And then we put the values in a list.

Conclusion

To get list of values for list of keys with Python dictionaries, we can use list comprehension.

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 *