Dictionary values() method in Python

Spread the love

values() method –

The values method returns a list of all the values in the dictionary.

Syntax of values method –

dict.values()

Let’s create a dictionary which contains prices of stocks.

In [1]: prices = {'icici': 710, 'reliance': 2617, 'airtel': 737}

To get all the prices of stocks, we can us the values method in dictionary.

In [2]: prices.values()
Out[2]: dict_values([710, 2617, 737])

Looping through all the values in a dictionary –

You can also loop through all the values in a dictionary using a for loop.


In [3]: for value in prices.values():
   ...:     print(value)
   ...:     
710
2617
737

Rating: 1 out of 5.

Leave a Reply