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.