Python Dictionaries 🎯

beginner
13 min

Python Dictionaries 🎯

Welcome to our comprehensive guide on Python Dictionaries! In this lesson, we'll learn about this powerful data structure, understand why they are essential, and explore their use in practical scenarios. Let's dive in!

What are Python Dictionaries? 📝

A dictionary in Python is a collection of key-value pairs. Think of it as a box where each item (value) is labeled with a specific name (key).

python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

In the example above, 'name', 'age', and 'city' are keys, and 'John', 30, and 'New York' are their corresponding values.

Why use Python Dictionaries? 💡

Dictionaries are useful because they allow us to store and access data quickly. Unlike lists, dictionaries can look up values using keys, making them ideal for efficient data retrieval.

Creating Python Dictionaries 📝

Creating a dictionary is as simple as assigning a new set of key-value pairs to a variable.

python
# Creating a dictionary my_dict = {'name': 'John', 'age': 30, 'city': 'New York'} # Accessing values print(my_dict['name']) # Output: John print(my_dict['age']) # Output: 30 print(my_dict['city']) # Output: New York

Adding Key-Value Pairs 📝

You can add new key-value pairs to an existing dictionary using the [] operator.

python
# Adding a new key-value pair my_dict['job'] = 'Developer' print(my_dict) # Output: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Developer'}

Modifying Existing Values 📝

You can modify the value of an existing key by reassigning it.

python
# Modifying an existing value my_dict['age'] = 35 print(my_dict) # Output: {'name': 'John', 'age': 35, 'city': 'New York', 'job': 'Developer'}

Checking if a Key Exists 📝

To check if a key exists in a dictionary, you can use the in keyword.

python
# Checking if a key exists if 'job' in my_dict: print('The key "job" exists.') # Output: The key "job" exists.

Removing a Key-Value Pair 📝

To remove a key-value pair, you can use the del keyword or the pop() method.

python
# Removing a key-value pair using del del my_dict['city'] print(my_dict) # Output: {'name': 'John', 'age': 35, 'job': 'Developer'} # Removing a key-value pair using pop() my_city = my_dict.pop('city') print(my_dict) # Output: {'name': 'John', 'age': 35, 'job': 'Developer'} print(my_city) # Output: New York

Copying Dictionaries 📝

To copy a dictionary, you can use the copy() method or the dict() function.

python
# Copying a dictionary using copy() my_dict_copy = my_dict.copy() print(my_dict_copy) # Output: {'name': 'John', 'age': 35, 'job': 'Developer'} # Copying a dictionary using dict() my_dict_copy_2 = dict(my_dict) print(my_dict_copy_2) # Output: {'name': 'John', 'age': 35, 'job': 'Developer'}

Looping Through Dictionaries 📝

You can loop through dictionaries using the for loop.

python
# Looping through a dictionary for key, value in my_dict.items(): print(f'Key: {key}, Value: {value}') # Output: # Key: name, Value: John # Key: age, Value: 35 # Key: job, Value: Developer
Quick Quiz
Question 1 of 1

What does a dictionary in Python store?


By the end of this lesson, you should have a solid understanding of Python Dictionaries. Now, let's put our knowledge into practice by building a simple address book application! Stay tuned for the next lesson. 🎯