Python Dictionary: A Guide for Beginners

wordpediax
4 min readNov 6, 2023

In the world of Python programming, dictionaries are a versatile and fundamental data structure that allows you to store and manipulate data in a highly flexible manner.

In this beginner’s guide, we will explore Python dictionaries, understand what they are, how to create them and provide practical examples to get you started.

What Are Dictionaries?

A dictionary in Python is an unordered collection of key-value pairs. Each key is unique and associated with a specific value. Think of a dictionary as a set of words and their definitions. Just like you look up a word in a real-world dictionary to find its meaning, in Python, you can look up a key to find its associated value.

Here’s a simple example of a dictionary:

my_dict = {
"apple": 1,
"banana": 2,
"cherry": 3
}

In this dictionary, the keys are "apple", "banana", and "cherry", and the corresponding values are 1, 2, and 3.

Creating Dictionaries

Dictionaries can be created in several ways:

1. Using Curly Braces

The most common way to create a dictionary is by enclosing key-value pairs within curly braces {}.

my_dict = {
"name": "Alice",
"age": 30,
"city": "New York"
}

2. Using the dict() Constructor

You can also create a dictionary using the dict() constructor by passing it a list of key-value pairs as tuples.

my_dict = dict([
("name", "Bob"),
("age", 25),
("city", "Los Angeles")
])

3. Using a Dictionary Comprehension

You can create dictionaries using dictionary comprehensions, a concise way to generate dictionaries from iterable data.

my_dict = {k: v for k, v in [("name", "Charlie"), ("age", 22), ("city", "Chicago")]}

4. Creating an Empty Dictionary

To create an empty dictionary, simply use empty curly braces {}.

empty_dict = {}

Accessing Dictionary Values

You can access values in a dictionary by referring to their keys. Here’s how to do it:

my_dict = {
"name": "David",
"age": 28,
"city": "Houston"
}

# Access values using keys
name = my_dict["name"]
age = my_dict["age"]
city = my_dict["city"]

print(name) # Output: David
print(age) # Output: 28
print(city) # Output: Houston

Modifying Dictionaries

Dictionaries are mutable, which means you can add, modify, or remove key-value pairs. Here are some common dictionary operations:

Adding Key-Value Pairs

You can add new key-value pairs to a dictionary.

my_dict = {
"name": "Eve",
"age": 35
}

# Add a new key-value pair
my_dict["city"] = "Miami"

print(my_dict)
# Output: {'name': 'Eve', 'age': 35, 'city': 'Miami'}

Modifying Values

You can change the value associated with a specific key.

my_dict = {
"name": "Frank",
"age": 40,
"city": "Phoenix"
}

# Update the value of the 'age' key
my_dict["age"] = 45

print(my_dict)
# Output: {'name': 'Frank', 'age': 45, 'city': 'Phoenix'}

Removing Key-Value Pairs

You can remove key-value pairs from a dictionary using the del statement.

my_dict = {
"name": "Grace",
"age": 50,
"city": "Seattle"
}

# Remove the 'city' key-value pair
del my_dict["city"]

print(my_dict)
# Output: {'name': 'Grace', 'age': 50}

You Might Like

Dictionary Methods

Dictionaries come with various built-in methods that allow you to perform operations on them. Let’s explore some of the most commonly used dictionary methods.

get()

The get() method allows you to retrieve the value associated with a key. It returns None if the key is not found.

Example:

my_dict = {
"name": "Hannah",
"age": 22
}

age = my_dict.get("age")
city = my_dict.get("city")

print(age) # Output: 22
print(city) # Output: None

In this example, we use the get() method to retrieve the value associated with the key "age" and "city."

keys()

The keys() method returns a list of all the keys in the dictionary.

Example:

my_dict = {
"name": "Isaac",
"age": 30,
"city": "San Francisco"
}

keys = my_dict.keys()

print(keys) # Output: dict_keys(['name', 'age', 'city'])

In this example, the keys() method is used to retrieve a list of keys.

values()

The values() method returns a list of all the values in the dictionary.

Example:

my_dict = {
"name": "Jane",
"age": 25,
"city": "Dallas"
}

values = my_dict.values()

print(values) # Output: dict_values(['Jane', 25, 'Dallas'])

In this example, the values() method is used to retrieve a list of values.

items()

The items() method returns a list of key-value pairs (tuples) in the dictionary.

Example:

my_dict = {
"name": "Kevin",
"age": 35,
"city": "Boston"
}

items = my_dict.items()

print(items) # Output: dict_items([('name', 'Kevin'), ('age', 35), ('city', 'Boston')])

In this example, the items() method is used to retrieve a list of key-value pairs.

You Might Like

Conclusion

Python dictionaries are a powerful data structure for storing and manipulating data. They allow you to work with key-value pairs efficiently, making them essential for a wide range of programming tasks.

Whether you’re storing user information, counting word frequencies, or categorizing data, dictionaries provide a flexible and intuitive way to manage and organize your data. As you continue your journey in Python programming, mastering dictionaries will be a valuable skill to have in your toolkit.

Want to explore more Python Articles?

--

--

wordpediax

I like to read and write articles on tech topics like Python, ML, AI. Beside this, I like to thread life into words and connect myself with nature.