Dictionaries in Python
A dictionary in Python is a collection of key-value pairs. Each key is associated with a value, and you can use the key to access the corresponding value. Dictionaries are unordered, meaning that the order of the items in a dictionary is not guaranteed.
Creating a dictionary
To create a dictionary in Python, you can use curly braces (
{}
) and separate the key-value pairs with colons (
:
). For example:
# Create a dictionary with three key-value pairs
person = {"name": "Alice", "age": 25, "city": "New York"}
# Print the dictionary
print(person)
In this example, we create a dictionary called
person
with three key-value pairs:
"name": "Alice"
,
"age": 25
, and
"city": "New York"
. We then print the dictionary to the screen using the
print()
function.
Accessing values in a dictionary
To access the value associated with a key in a dictionary, you can use the square bracket notation (
[]
) and provide the key as the index. For example:
# Create a dictionary with three key-value pairs
person = {"name": "Alice", "age": 25, "city": "New York"}
# Access the value associated with the "name" key
name = person["name"]
# Print the value
print(name)
In this example, we access the value associated with the
"name"
key in the
person
dictionary by using the square bracket notation and providing
"name"
as the index. We then assign the value to a variable called
name
and print it to the screen.
Modifying values in a dictionary
To modify the value associated with a key in a dictionary, you can use the square bracket notation and provide the key as the index. For example:
# Create a dictionary with three key-value pairs
person = {"name": "Alice", "age": 25, "city": "New York"}
# Modify the value associated with the "age" key
person["age"] = 30
# Print the modified dictionary
print(person)
In this example, we modify the value associated with the
"age"
key in the
person
dictionary by using the square bracket notation and providing
"age"
as the index. We then assign the new value of
30
to the key-value pair and print the modified dictionary to the screen.
Looping through a dictionary
To loop through the key-value pairs in a dictionary, you can use a for loop and the
items()
method. For example:
# Looping through a dictionary
for (key, value) in items():
print(key, value)
In this example, we use a for loop to iterate over the key-value pairs in the
person
dictionary. We use the
items()
method to get a list of tuples containing the key-value pairs, and then unpack each tuple into separate variables called
key
and
value
. We then print each key and value to the screen.
Dictionary methods
Python dictionaries provide several built-in methods that you can use to manipulate the dictionary. Some of the most commonly used methods are:
-
get(key, default=None)
: Returns the value associated with the specified key. If the key does not exist, returns the default value (orNone
if no default value is specified). -
keys()
: Returns a list of all the keys in the dictionary. -
values()
: Returns a list of all the values in the dictionary. -
items()
: Returns a list of tuples containing the key-value pairs in the dictionary. -
clear()
: Removes all the key-value pairs from the dictionary. -
pop(key, default=None)
: Removes the key-value pair associated with the specified key and returns the value. If the key does not exist, returns the default value (or raises aKeyError
if no default value is specified). -
popitem()
: Removes and returns an arbitrary key-value pair from the dictionary. Raises aKeyError
if the dictionary is empty. -
update(other)
: Updates the dictionary with the key-value pairs from another dictionary or an iterable of key-value pairs.
Conclusion
Dictionaries are a powerful data structure in Python that allow you to store and manipulate collections of key-value pairs. By understanding how to create, access, modify, and loop through dictionaries, you can write more expressive and efficient code.