Inspired Python
>>> d = {"first_name": "Cosmo", "last_name": "Kramer"}
# Create a `dict_keys` view of the keys
>>> keys = d.keys()
>>> keys
dict_keys(['first_name', 'last_name'])
# Now remove a key...
>>> del d['last_name']
# ... and now the view of the keys reflects the new contents
>>> keys

Python Dictionary Tricks: keys(), values() and items() are dynamic

When you get the keys(), values() or items() from a dictionary, you are given a view of the elements in the dictionary. If you modify the dictionary the reference you have is updated to reflect this.

Be Inspired Get Python tips sent to your inbox

We'll tell you about the latest courses and articles.

Absolutely no spam. We promise!