Built-in functions supported by dictionary view objects


The dictionary view objects include dict.keys(), dict.values(), dict.items(). They are used to obtain the dynamic view of the dictionary elements in python. These objects reflect the changes made to the dictionary.

To retrieve some data or perform various operations on these view objects, there are 5 build-in functions in python that are supported. They are as follows −

  • len(obj)

  • iter(obj)

  • reversed(obj)

  • sorted(obj)

  • ist(obj)

We will discuss about all the functions mentioned above in this article.

The len(obj) method

The len(obj) method takes a view object as a parameter and returns the number of items in the dictionary.

Example

Following is an example to return the total number of items in a dictionary −

d = {'g': 1, 'b': 2, 'c': 3, 'd': 4} k=d.keys() print (len(k)) v=d.values() print (len(v)) i=d.items() print (len(i))

Output

The output of the above code is given below −

4
4
4

The iter(obj) method

Using the iter(obj) method, the keys of a dictionary are iterated over the order in which they are inserted. It takes any view object as a parameter and then it returns an iterator over the keys, values, or in the dictionary.

Example 1

Following is an example to iterate over keys for converting iterator to list using list() constructor −

d = {'g': 1, 'b': 5, 'c': 3, 'd': 4} k=d.keys() print (iter(d)) #Converting iterator to list print (list(iter(d)))

Output

<dict_keyiterator object at 0x7f825131df40>
['g', 'b', 'c', 'd']

Example 2

Following is an example to iterate over the values for accessing elements in iterator using for loop −

d = {'g': 1, 'b': 5, 'c': 3, 'd': 4} v=d.values() print (iter(v)) #Accessing elements in iterator using for loop for i in iter(v): print (i)

Output

<dict_valueiterator object at 0x7f0174c31f90>
1
5
3
4

Example 3

Following is an example to iterate over the items to access the elements in the iterator using the next() function −

d = {'g': 1, 'b': 5, 'c': 3, 'd': 4} i=d.items() i1=iter(i) #Accessing elements in itertaor using next() fucntion print (next(i1)) print (next(i1)) print (next(i1))

Output

('g', 1)
('b', 5)
('c', 3)

The reversed(obj) method

In contrast to the iter(obj) method, the reversed(obj) runs an iterator in the reversed order of the insertion. This built-in function takes a view object as a parameter and returns a reverse iterator over the keys, values, or items of the dictionary.

Example

Following is an example to return the keys, values and items of a dictionary in the reverse order −

d = {'g': 1, 'b': 5, 'c': 3, 'd': 4} k=reversed(d.keys()) print (list(k)) v=reversed(d.values()) print (list(v)) i=reversed(d.items()) print (list(i)) #return a reversed iterator of keys alone d1=reversed(d) print (list(d1))

Output

['d', 'c', 'b', 'g']
[4, 3, 5, 1]
[('d', 4), ('c', 3), ('b', 5), ('g', 1)]
['d', 'c', 'b', 'g']

The sorted(obj) method

The sorted(obj) method takes the view objects as a parameter and returns a corresponding sorted list over the keys, values, or items of the dictionary.

Example

Following is an example to print the items of a dictionary in the alphabetical order −

d = {'g': 1, 'b': 5, 'c': 3, 'd': 4} print (sorted(d.keys())) print (sorted(d.values())) print (sorted(d.items())) d1=print (sorted(d))

Output

['b', 'c', 'd', 'g']
[1, 3, 4, 5]
[('b', 5), ('c', 3), ('d', 4), ('g', 1)]
['b', 'c', 'd', 'g']

The list(obj) method

The list(obj) function takes the view objects as parameters and return a list over the keys, values, or items of the dictionary.

Example

Following is an example to print the items, keys and values of a dictionary −

d = {'g': 1, 'b': 5, 'c': 3, 'd': 4} print (list(d.keys())) print (list(d.values())) print (list(d.items())) d1=print (list(d))

Output

['g', 'b', 'c', 'd']
[1, 5, 3, 4]
[('g', 1), ('b', 5), ('c', 3), ('d', 4)]
['g', 'b', 'c', 'd']

Updated on: 25-Aug-2022

374 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements