What are Python dictionary view objects?



A dictionary is a data structure that is used for storing a group of objects. A dictionary is having set of keys and every key has a single allocated value. When presented with a key, the dictionary will return the allocated value.

In the python dictionaries, some of the methods return view objects. This view objects are dynamic view objects, meaning that when we made some changes in the dictionary then the view reflects all these changes.

The main view objects of dictionary in python are keys, values and items. They provide a non-constant view of the dictionary’s entries.

Keys holds all the keys of the dictionary, the values objects holds all the values, whereas the items holds the key-value pairs.

keys

The view object keys represent all the keys in a dictionary. You can retrieve the contents of this object using the keys() method.

If you print the view object keys, it displays the new list of all the keys in the dictionary in order of insertion.

Once we retrieve all the keys using the keys() method into a variable (say keys), if we update the dictionary (i.e. add or delete keys) the changes will be reflected in the variable.

Example 1

In the following example we are trying to retrieve all the keys of a dictionary using the keys() method.

d = {'a': 5, 'b': 2, 'c': 0, 'd': 7} k = d.keys() print(k) print(type(k))

Output

dict_keys(['a', 'b', 'c', 'd'])
<class 'dict_keys'>

Example 2

Updates will be reflected in the retrieved keys

In the following program, we have created a dictionary with 4 key value pairs in it. Retrieved the keys from the dictionary and stored them in variable keys.

  • Later we have added an element to the dictionary.

  • If print the value of the variable keys after the update. You can find the key of the added element there.

dictionary = {'a': 5, 'b': 2, 'c': 0, 'd': 7} keys = dictionary.keys() print("Keys before Dictionary Updation:", keys) # updating the dictionary dictionary['e'] = 5 print("listing updated dictionary:", dictionary) # dynamic view object is updated automatically print("Keys after Dictionary Updation:", keys)

Output

Keys before Dictionary Updation: dict_keys(['a', 'b', 'c', 'd'])
listing updated dictionary: {'a': 5, 'b': 2, 'c': 0, 'd': 7, 'e': 5}
Keys after Dictionary Updation: dict_keys(['a', 'b', 'c', 'd', 'e'])

Example 3

Following is another example of the keys() function in here we are removing a key and verifying the contents of the retrieved keys.

dict = {'Madhu': 1001, 'Raju': 1002, 'Radha': 2003, 'Yadav': 9004} print("The dictionary before removing a key is : " + str(dict)) removed_value = dict.pop('Raju') print("The value of removed key is : " + str(removed_value)) # Printing dictionary after removal print("The dictionary after removing key is : " + str(dict))

Output

The dictionary before removing a key is : {'Madhu': 1001, 'Raju': 1002, 'Radha': 2003, 'Yadav': 9004}
The value of removed key is : 1002
The dictionary after removing key is : {'Madhu': 1001, 'Radha': 2003, 'Yadav': 9004}

Values

values() is an built in method of Python programming language which returns a view object. That view object contains the value of the dictionary as a list.

Example 1

Following is an example to print the values of a dictionary −

d = {'a': 5, 'b': 2, 'c': 0, 'd': 7} v = d.values() print(v) print(type(v))

Output

As we can see in the following output, all the values of the dictionary gets printed −

dict_values([5, 2, 0, 7])
<class 'dict_values'>

Example 2- Updating the dictionary

Following is an example to update the dictionary by adding values to it −

d = {'a': 5, 'b': 2, 'c': 0, 'd': 7} v = d.values() print('values before before updating : ', v) d['e'] = 5 print('Dictionary after adding new value', d) # dynamic view object is updated automatically print('value of the updated dictionary: ', v)

Output

values before before updating : dict_values([5, 2, 0, 7]) Dictionary after adding new value {'a': 5, 'b': 2, 'c': 0, 'd': 7, 'e': 5} value of the updated dictionary: dict_values([5, 2, 0, 7, 5])

Example 3

Following is an example to sum up the values of a dictionary −

#Adding the values in the dictionary d1 = {"apple" : 5000, "gold" : 60000, "silver" : 50000} list1 = d1.values() a=(sum(list1)) print('Total value:',a)

Output

Total value: 115000

Items

The items() method returns a new view of the dictionary’s items ((key, value) pairs) as a list with all dictionary keys with values.

Syntax

Following is the syntax of items −

dictionary.items()

Example 1

Following is an example to print all the items of a dictionary −

d = {'a': 5, 'b': 2, 'c': 0, 'd': 7} i = d.items() print(i) print(type(i))

Output

dict_items([('a', 5), ('b', 2), ('c', 0), ('d', 7)])
<class 'dict_items'>

Example 2

Following is an example to remove item from the dictionary −

d1 = {'a': 5, 'b': 2, 'c': 0, 'd': 7} # Printing all the Dictionarie’s item print("Original Dictionary items:", d1) items = d1.items() # Delete an item from dictionary del[d1['c']] #Printing Updtaed dictionary print('Updated Dictionary:',items)

Output

Original Dictionary items: {'a': 5, 'b': 2, 'c': 0, 'd': 7}
Updated Dictionary: dict_items([('a', 5), ('b', 2), ('d', 7)])

Example 3

The following example shows the modification of items() of a dictionary −

car = { "brand": "Ford", "model": "Mustang", "year": 1964 } print('Orginal dictionary: ',car) x = car.items() car["year"] = 2018 print('updated dictionary: ',x)

Output

As we can see in the following output, the year has been modified from 1964 to 2018 −

Orginal dictionary: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
updated dictionary: dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2018)])

Advertisements