Dictionary values() method Python

The Python dictionary values() method return all the values of the dictionary object.If you want all the values in the dictionary use this method.Note this method does not accept any argument.

Link :Python dictionary data type

>>> d=dict( zip("Textual" , "Base in"))
>>> d
{'T': 'B', 'e': 'a', 'x': 's', 't': 'e', 'u': ' ', 'a': 'i', 'l': 'n'}
>>> d.values()
dict_values(['B', 'a', 's', 'e', ' ', 'i', 'n'])
>>> #Another example
>>> d1={'Tempt':'Hex' , 345:"Text" , 'Doom':1 , 'All':'New'}
>>> d1.values()
dict_values(['Hex', 'Text', 1, 'New'])

The method is pretty simple to use and understand.

Checking value in a values sequence

If you want o checl if certain value exit in the dictionary object you can use the keyword ‘in’.The keyword ‘in’ checks if any given value exist in the dictionary object,if it does True is return else False.

To use the keyword ‘in’ we will follow the format:
Value_to_check in dictionary_object_sequence
The Value_to_check is the value which is to be checked in the object.
The dictionary_object_sequence is the sequence of values of the dictionary object obtain by calling values().

>>> d=dict( zip("Textual" , "Base in"))
>>> 's' in d.values()
>>> ' ' in d.values()
>>> 'I' in d.values()
# Another example 
>>> d1={'Tempt':'Hex' , 345:"Text" , 'Doom':1 , 'All':'New'}
>>> 'tempt' in d1.values()
>>> 'All' in d1.values()
>>> "Text" in d1.values()

When the value does not exist False is return if it does exist True is returned.

Leave a Reply

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