Do You Know That dict.keys() Is a Dynamic View Object?
It’s also the case for dict.values() and dict.items(). Learn what they are
We use dictionaries, particularly the built-in
dict data type, almost everywhere in our Python projects.
As an essential data structure, it stores its elements as key-value pairs by implementing a hash table that allows efficient insertion, deletion, and lookup. Some common usages are shown below.
The keys(), values(), and items() Methods
Iteration is a standard job we do with a
dict and other iterables. The three most common ways to generate iterables from a
dict are using the built-in functions
keys()method returns a set-like view object of the
dict_keystype that consists of all the keys of the calling
dict, whose keys are supposed to be unique and hashable.
values()method returns a view object of the
dict_valuestype that consists of all the values of the calling
dict. This view object can have duplicate values, as a
dict’s values don’t have to be unique.
items()method returns a view object of the
dict_itemstype that consists of all the key-value pairs of the
All of these methods can be used to iterate a
dict. Let’s see their respective usages in the iteration in the example below.
The View Objects
People who have some database knowledge are probably familiar with the concept of views, Wikipedia’s definition of which is below.
“In a database, a view is the result set of a stored query on the data, which the database users can query just as they would in a persistent database collection object.
as a result set, it is a virtual table computed or collated dynamically from data in the database when access to that view is requested. Changes applied to the data in a relevant underlying table are reflected in the data shown in subsequent invocations of the view.”
The dictionary view objects obtained using the
items() methods are similar to a view in a database.
In the example below, you can see that we create a view object called
letters_keys using the
After updating the dict by inserting another key-value pair, we can see that the view
letter_keys gets updated simultaneously. This view is the same object and consistently has the same memory address.
Besides the usage of these view objects in the iteration and their dynamic updating feature, as shown above, there are three more things to note.
- These view objects can’t be indexed. Although they look like a list or a set, they don’t support indexing. So, don’t find yourself surprised if you see the pertinent error when you do that.
- Membership testing is available to all these three view object types, namely
dict_items. So, we can conveniently check if the
dicthas a particular key, value, or key-value pair.
dict_keystype supports some set-like operations. It is because the keys in a
dictare unique and hashable, which is similar to the items in a set being unique and hashable.
In this article, we learned that the values returned from a
items() methods are dictionary view objects. Here are some key takeaways.
- They are iterables and thus can be used for iteration.
- They are dynamic and will update as the dictionary updates.
- We can’t use indexing to retrieve a particular item of the view objects.
- The view objects support membership testing.
Thanks for reading!