CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

PYTHON - Dictionary View Objects

Python - Dictionary View Objects

Theitems(),keys(), andvalues()methods of dict class return view objects. These views are refreshed dynamically whenever any change occurs in the contents of their sourcedictionaryobject.

The items() Method

Theitems()method returns a dict_items view object. It contains alistoftuples, each tuple made up of respective key, value pairs.

Syntax

Following is the syntax of the items() method −

PYTHON CODE
Obj = dict.items()

Return value

Theitems()method returns dict_items object which is a dynamic view of (key,value) tuples.

Example

In the following example, we first obtain the dict_items object with items() method and check how it is dynamically updated when the dictionary object is updated.

PYTHON CODE
numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
obj = numbers.items()
print ('type of obj: ', type(obj))
print (obj)
print ("update numbers dictionary")
numbers.update({50:"Fifty"})
print ("View automatically updated")
print (obj)

It will produce the following output −

PYTHON CODE
type of obj: <class 'dict_items'>
dict_items([(10, 'Ten'), (20, 'Twenty'), (30, 'Thirty'), (40, 'Forty')])
update numbers dictionary
View automatically updated
dict_items([(10, 'Ten'), (20, 'Twenty'), (30, 'Thirty'), (40, 'Forty'), (50, 'Fifty')])

The keys() Method

Thekeys()method of dict class returns dict_keys object which is a list of all keys defined in the dictionary. It is a view object, as it gets automatically updated whenever any update action is done on the dictionary object.

Syntax

Following is the syntax of the keys() method −

PYTHON CODE
Obj = dict.keys()

Return value

The keys() method returns dict_keys object which is a view of keys in the dictionary.

Example

In this example, we are creating a dictionary named “numbers” with integer keys and their corresponding string values. Then, we obtain a view object “obj” of the keys using the keys() method, and retrieve its type and content −

PYTHON CODE
numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
obj = numbers.keys()
print ('type of obj: ', type(obj))
print (obj)
print ("update numbers dictionary")
numbers.update({50:"Fifty"})
print ("View automatically updated")
print (obj)

It will produce the followingoutput−

PYTHON CODE
type of obj: <class 'dict_keys'>
dict_keys([10, 20, 30, 40])
update numbers dictionary
View automatically updated
dict_keys([10, 20, 30, 40, 50])

The values() Method

Thevalues()method returns a view of all the values present in the dictionary. The object is of dict_value type, which gets automatically updated.

Syntax

Following is the syntax of the values() method −

PYTHON CODE
Obj = dict.values()

Return value

The values() method returns a dict_values view of all the values present in the dictionary.

Example

In the example below, we obtain a view object “obj” of the values using the values() method from the “numbers” dictionary −

PYTHON CODE
numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
obj = numbers.values()
print ('type of obj: ', type(obj))
print (obj)
print ("update numbers dictionary")
numbers.update({50:"Fifty"})
print ("View automatically updated")
print (obj)

It will produce the followingoutput−

PYTHON CODE
type of obj: <class 'dict_values'>
dict_values(['Ten', 'Twenty', 'Thirty', 'Forty'])
update numbers dictionary
View automatically updated
dict_values(['Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty'])