Python: Dictionaries

Python dictionaries are data structures organized as a collection of key and value pairs. These mapping objects are hash table implementations with 0(1) retrieval. Keys act as unique references to their associated values. Dictionaries store values to blocks of memory, which are directly accessed by key. We can use any immutable type or any tuple containing immutable types as keys. A dictionary’s keys must be unique, while its values are arbitrary objects.

Dictionary Methods

MethodDescription
.clear()Empties the dictionary
.copy()Returns a copy of the dictionary
.fromkeys()Returns a dictionary with the specified keys
.get()Returns the corresponding value of the specified key
.items()Returns a list containing key: value pairs
.keys()Returns a list comprised of the entire dictionary’s keys
.pop()Returns and removes a key: value pair, specified by key
.popitem()Returns the last key: value pair
.setdefault()Returns the corresponding value of the specified key
If key does not exist, insert key: value pair
.update()Updates the dictionary with a specified key: value pair
.values()Returns a list comprised of the entire dictionary’s values

Dictionary Examples

# Build a dictionary with empty braces.
our_dictionary = {}
print(our_dictionary)
# Build a dictionary incrementally.
our_dictionary = {}
our_dictionary["key 1"] = "value 1"
print(our_dictionary)
# Include comma-separated key: value pairs to add to the dictionary.
our_dictionary = {"key 1": "value 1", "key 2": True}
print(our_dictionary)
# Add an item to a dictionary with update.
our_dictionary = {"key 1": "value 1", "key 2": True}
our_dictionary.update({"key 3": 1})
print(our_dictionary)
# List a dictionary’s keys.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
key_list = list(our_dictionary)
print(key_list)
# List a dictionary’s values.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
value_list = list(our_dictionary.values())
print(value_list)
# Retrieve a dictionary's contents.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
item_list = list(our_dictionary.items())
print(item_list)
# Sort a dictionary by key.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
sorted_dictionary = sorted(our_dictionary)
print(sorted_dictionary)
# Delete an item from a dictionary.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
del our_dictionary['key 1']
print(our_dictionary)
# Pop the last item from a dictionary.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
popped_item = our_dictionary.popitem()
print(popped_item)
# Pop an item from a dictionary by key.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
popped_item = our_dictionary.pop("key 1")
print(popped_item)
# Unpack a dictionary's keys.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
key_1, key_2, key_3 = our_dictionary.keys()
print(key_1, key_2, key_3)
# Unpack a dictionary's values.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
value_1, value_2, value_3 = our_dictionary.values()
print(value_1, value_2, value_3)
# Loop through a dictionary.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
for key, value in our_dictionary.items():
    print(key, value)
# Loop through a dictionary in sorted order.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
for key in sorted(our_dictionary.keys()):
    print(key)
# Loop through a dictionary in reverse order.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
for key in reversed(our_dictionary.keys()):
    print(key)
# Search a dictionary's keys.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
key_search = "key 1" in our_dictionary
print(key_search)
# Get a value from a dictionary by key with get.
our_dictionary = {"key 1": "value 1", "key 2": True, "key 3": 1}
return_value = our_dictionary.get("key 2")
print(return_value)
# Use a dictionary comprehension.
import string

our_dictionary = {k: v for k, v in enumerate(string.ascii_lowercase)}
print(our_dictionary)
# Use a dictionary constructor.
our_dictionary = dict([('key 1', "value 1"), ("key 2", True), ("key 3", 1)])
print(our_dictionary)

About the author

Shane Bellone

At the intersection of capricious and whimsy.

Add comment