Python Dictionary – Get Values as List

Dictionary is a collection of key:value pairs. We can get all the values in the dictionary as a Python List.

dict.values() returns an iterable of type dict_values(). We can convert this into a list using list().

Also, we can use * operator, which unpacks an iterable. Unpack dict or dict.values() in [] using * operator. It creates a list with dictionary’s values in it.

Example 1 – Get Values of Dictionary as List using dict.values()

In this example, we will create a Dictionary with some initial entries, and then get all the values into a list using dict.values() method.

Python Program

</>
Copy
#initialize dictionary
myDictionary = {
    'tallest building':'Burj Khalifa',
    'longest river':'The Nile',
    'biggest ocean':'The Pacific Ocean'
}

# get values as list
values = list(myDictionary.values())

#print the list
print(values)

Output

['Burj Khalifa', 'The Nile', 'The Pacific Ocean']

We got all the values in dictionary as a list.

Example 2 – Unpack Dictionary Values in a List

* operator unpacks a sequence. So, we will unpack the dictionary values in [], which will create a list.

Python Program

</>
Copy
#initialize dictionary
myDictionary = {
    'tallest building':'Burj Khalifa',
    'longest river':'The Nile',
    'biggest ocean':'The Pacific Ocean'
}

# get values as list
values = [*myDictionary.values()]

#print the list
print(values)

Output

['Burj Khalifa', 'The Nile', 'The Pacific Ocean']

Conclusion

In this Python Tutorial, we learned how to get the values in a dictionary as list.