Working with Dictionaries in Python

Use Cases

Dictionaries are Python's native data type for storing key-value mapped data. They are mutable objects, and all keys in a valid ditcionary must be unique.

Creating a Dictionary

Core characteristics of Python dictionaries:

  • Wrapped in curly braces {}
  • All data is stored as individual key-value pairs
  • Pairs are separated by commas
# Create a pre-populated dictionary
user = {'name': 'tom', 'age': 20, 'gender': 'male'}
# Two common ways to create an empty dictionary
empty_dict_a = {}
empty_dict_b = dict()

Common Operations

Add or Update Entries

The syntax for adding and updating entries is the same: dictionary[key] = value. If the key already exists, its corresponding value will be overwritten. If the key does not exist, a new key-value pair will be added to the dictionary.

user = {'name': 'tom', 'age': 20, 'gender': 'male'}
# Update the value of an existing key
user['name'] = 'lily'
print(user)
# Output: {'name': 'lily', 'age': 20, 'gender': 'male'}

# Add a new key-value pair
user['employee_id'] = '001'
print(user)
# Output: {'name': 'lily', 'age': 20, 'gender': 'male', 'employee_id': '001'}

Delete Entries

Use del (or del()) to remove an entire dictionary variable, or delete a specific key-value pair:

user = {'name': 'tom', 'age': 20, 'gender': 'male'}
# Delete a single key-value pair
del user['name']
print(user)
# Output: {'age': 20, 'gender': 'male'}

del(user['age'])
print(user)
# Output: {'gender': 'male'}

# Delete the entire dictionary variable
del user
print(user)
# Error: name 'user' is not defined

To clear all entries from a dictionary without deleting the variable itself, use the clear() method:

user = {'name': 'tom', 'age': 20, 'gender': 'male'}
user.clear()
print(user)
# Output: {}

Modify Entries

Modifying existing entries follows the same syntax as adding new entries: dictionary[key] = value. If the target key exists, its value will be updated; if not, a new entry will be added.

Look Up Values

Direct Key Lookup

You can access a value directly by its key using square bracket notation:

user = {'name': 'tom', 'age': 20, 'gender': 'male'}
print(user['name'])
# Output: tom

print(user['id'])
# Error: Key not found

Note that this method throws an error if the requested key does not exist in the dictionary.

Safe Lookup with get()

For error-free lookups, use the built-in get() method, with the syntax: dictionary.get(key, default_value)

user = {'name': 'tom', 'age': 20, 'gender': 'male'}
print(user.get('name'))
# Output: tom

print(user.get('id', 'Key does not exist'))
# Output: Key does not exist

If the key is not found and no custom default is provided, get() will return None instead of throwing an error.

Dictionary Traversal

Traverse all keys

Use the keys() method to get an iterable collection of all keys in the dictionary:

product = {'name': 'laptop', 'price': 999, 'stock': 15}
for key in product.keys():
    print(key)
# Output:
# name
# price
# stock

Traverse all values

Use the values() method to iterate over every stored value directly:

product = {'name': 'laptop', 'price': 999, 'stock': 15}
for value in product.values():
    print(value)
# Output:
# laptop
# 999
# 15

Traverse all entries

Use the items() method to iterate over each full key-value pair as a tuple:

product = {'name': 'laptop', 'price': 999, 'stock': 15}
for entry in product.items():
    print(entry)
# Output:
# ('name', 'laptop')
# ('price', 999)
# ('stock', 15)

You can also unpack the key and value directly in the loop for easier access:

product = {'name': 'laptop', 'price': 999, 'stock': 15}
for key, value in product.items():
    print(key, value)
# Output:
# name laptop
# price 999
# stock 15

Tags: python Dictionaries Data Structures Python Basics

Posted on Tue, 15 Sep 2026 16:10:08 +0000 by prasadharischandra