Skip to content

Python Dictionary Cheat Sheet: Effortlessly Master the Key-value Data Structure

[

Python Dictionary Cheat Sheet

Introduction

Python is a powerful programming language that provides various data structures to handle and manipulate data efficiently. One of the most commonly used data structures in Python is a dictionary. A dictionary is an unordered collection of key-value pairs, where each key is unique. In this cheat sheet, we will explore various operations and functionalities of dictionaries in Python.

Creating a Dictionary

To create a dictionary in Python, you can use curly braces {} and separate the key-value pairs with a colon (:). Let’s create a dictionary to store user information:

user = {
"name": "John Doe",
"age": 25,
"email": "johndoe@example.com"
}

Accessing Dictionary Values

You can access the values in a dictionary by referring to their respective keys. Let’s access the name and age of the user:

print(user["name"]) # Output: John Doe
print(user["age"]) # Output: 25

Modifying Dictionary Values

You can modify the values in a dictionary by assigning a new value to a specific key. Let’s update the user’s email address:

user["email"] = "johndoe123@example.com"
print(user["email"]) # Output: johndoe123@example.com

Checking if a Key Exists

To check if a specific key exists in a dictionary, you can use the in keyword. Let’s check if the user’s name is present in the dictionary:

if "name" in user:
print("Name exists.")
else:
print("Name does not exist.")

Dictionary Length

You can find the number of key-value pairs in a dictionary using the len() function. Let’s find the length of the user dictionary:

print(len(user)) # Output: 3

Removing Key-Value Pairs

To remove a key-value pair from a dictionary, you can use the del keyword and specify the key. Let’s remove the email key-value pair from the user dictionary:

del user["email"]
print(user) # Output: {"name": "John Doe", "age": 25}

Dictionary Methods

Python provides several built-in methods to perform various operations on dictionaries. Here are some commonly used methods:

keys()

The keys() method returns a list containing all the keys in a dictionary. Let’s print all the keys in the user dictionary:

print(user.keys()) # Output: ["name", "age"]

values()

The values() method returns a list containing all the values in a dictionary. Let’s print all the values in the user dictionary:

print(user.values()) # Output: ["John Doe", 25]

items()

The items() method returns a list of tuples containing all the key-value pairs in a dictionary. Let’s print all the key-value pairs in the user dictionary:

print(user.items()) # Output: [("name", "John Doe"), ("age", 25)]

Looping Through a Dictionary

You can iterate over the keys, values, or items of a dictionary using a for loop. Let’s print all the keys and values of the user dictionary:

for key in user.keys():
print(key)
for value in user.values():
print(value)
for key, value in user.items():
print(key, value)

Conclusion

This Python dictionary cheat sheet provides an overview of essential operations and functionalities of dictionaries in Python. By using dictionaries, you can easily store and access data through key-value pairs. Experiment with the examples provided to further enhance your Python programming skills.