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

Yong Cui, Ph.D.
Mar 18 · 3 min read
Image for post
Image for post

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(), values(), and items().

  • The keys() method returns a set-like view object of the dict_keys type that consists of all the keys of the calling dict, whose keys are supposed to be unique and hashable.
  • The values() method returns a view object of the dict_values type 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.
  • The items() method returns a view object of the dict_items type that consists of all the key-value pairs of the calling dict.

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 keys(), values(), and 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 keys() method.

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.

  1. 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.
  2. Membership testing is available to all these three view object types, namely dict_keys, dict_values, and dict_items. So, we can conveniently check if the dict has a particular key, value, or key-value pair.
  3. The dict_keys type supports some set-like operations. It is because the keys in a dict are unique and hashable, which is similar to the items in a set being unique and hashable.

Takeaways

In this article, we learned that the values returned from a dict’s keys(), values(), and 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!

Better Programming

Advice for programmers.

Thanks to Zack Shapiro

Yong Cui, Ph.D.

Written by

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

Better Programming

Advice for programmers.

Yong Cui, Ph.D.

Written by

Work at the nexus of biomedicine, data science & mobile dev. Love to write on these technological topics. Follow me @ycui01 on Twitter to get latest articles.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store