Lesson 6 of 30
In Progress

Creating and Accessing Dictionaries

In Python, dictionaries are a data type that allow you to store and manipulate a collection of key-value pairs. In this article, we will introduce you to the basics of dictionaries and show you how to create and access dictionaries in Python.

Creating a Dictionary

To create a dictionary in Python, you can use the “dict()” function or enclose a comma-separated list of key-value pairs in curly braces ({}).

For example:

# using the dict() function
info = dict(name="John", age=30, city="New York")

# using curly braces
info = {"name": "John", "age": 30, "city": "New York"}

You can also create an empty dictionary using the “dict()” function or curly braces, and then add items to the dictionary using the square bracket notation.

# using the dict() function
info = dict()
info["name"] = "John"
info["age"] = 30
info["city"] = "New York"

# using curly braces
info = {}
info["name"] = "John"
info["age"] = 30
info["city"] = "New York"

Accessing Dictionary Items

To access an item in a dictionary, you can use the square bracket notation and the key of the item you want to access. For example:

info = {"name": "John", "age": 30, "city": "New York"}

print(info["name"])    # prints "John"
print(info["age"])     # prints 30
print(info["city"])    # prints "New York"

You can also use the “get()” method to access an item in a dictionary. The “get()” method takes the key of the item as an argument, and returns the value of the item. If the key is not found in the dictionary, the “get()” method returns a default value specified as an optional argument.

For example:

info = {"name": "John", "age": 30, "city": "New York"}

print(info.get("name"))    # prints "John"
print(info.get("age"))     # prints 30
print(info.get("city"))    # prints "New York"
print(info.get("country", "United States"))    # prints "United States"

Modifying Dictionary Items

To modify an item in a dictionary, you can use the square bracket notation to assign a new value to the item. For example:

info = {"name": "John", "age": 30, "city": "New York"} 
info["age"] = 31 
print(info)  # prints {"name": "John", "age": 31, "city": "New York"}

You can also use the “update()” method to add or modify multiple items in a dictionary. The “update()” method takes a dictionary as an argument and adds the key-value pairs of the argument dictionary to the original dictionary. If a key already exists in the original dictionary, the value is updated with the value from the argument dictionary.

For example:

info = {"name": "John", "age": 30, "city": "New York"}
info.update({"age": 31, "country": "United States"})
print(info)    # prints {"name": "John", "age": 31, "city": "New York", "country": "United States"}

Deleting Dictionary Items

To delete an item from a dictionary, you can use the “del” statement and the key of the item you want to delete. For example:

info = {"name": "John", "age": 30, "city": "New York"}
del info["age"]
print(info)    # prints {"name": "John", "city": "New York"}

You can also use the “pop()” method to delete an item from a dictionary and store its value in a variable. The “pop()” method takes the key of the item as an argument, and removes the item from the dictionary. If the key is not found in the dictionary, the “pop()” method returns a default value specified as an optional argument.

For example:

info = {"name": "John", "age": 30, "city": "New York"}
age = info.pop("age", 0)
print(info)    # prints {"name": "John", "city": "New York"}
print(age)     # prints 30

Conclusion

Dictionaries are a powerful data type in Python that allow you to store and manipulate key-value pairs. By learning how to create and access dictionaries, as well as how to modify and delete items from a dictionary, you will be able to use dictionaries effectively in your Python programs.

In our “Introduction to Python” course, we will cover these concepts in more depth and show you how to use them to solve real-world problems. We hope you will join us on this journey!

Exercises

Here are some exercises with solutions to help you practice what you just learned:

How do you create a dictionary in Python?

To create a dictionary in Python, you can use the “dict()” function or enclose a comma-separated list of key-value pairs in curly braces ({}).

For example:

# using the dict() function
info = dict(name="John", age=30, city="New York")

# using curly braces
info = {"name": "John", "age": 30, "city": "New York"}

You can also create an empty dictionary using the “dict()” function or curly braces, and then add items to the dictionary using the square bracket notation.

# using the dict() function
info = dict()
info["name"] = "John"
info["age"] = 30
info["city"] = "New York"

# using curly braces
info = {}
info["name"] = "John"
info["age"] = 30
info["city"] = "New York"

How do you access an item in a dictionary in Python?

To access an item in a dictionary in Python, you can use the square bracket notation and the key of the item you want to access. For example:

info = {"name": "John", "age": 30, "city": "New York"}

print(info["name"])    # prints "John"
print(info["age"])     # prints 30
print(info["city"])    # prints "New York"

You can also use the “get()” method to access an item in a dictionary. The “get()” method takes the key of the item as an argument, and returns the value of the item. If the key is not found in the dictionary, the “get()” method returns a default value specified as an optional argument.

For example:

info = {"name": "John", "age": 30, "city": "New York"}

print(info.get("name"))    # prints "John"
print(info.get("age"))     # prints 30
print(info.get("city"))    # prints "New York"
print(info.get("country", "United States"))    # prints "United States"

How do you modify an item in a dictionary in Python?

To modify an item in a dictionary in Python, you can use the square bracket notation to assign a new value to the item. For example:

info = {"name": "John", "age": 30, "city": "New York"}
info["age"] = 31
print(info)    # prints {"name": "John", "age": 31, "city": "New York"}

You can also use the “update()” method to add or modify multiple items in a dictionary. The “update()” method takes a dictionary as an argument and adds the key-value pairs of the argument dictionary to the original dictionary. If a key already exists in the original dictionary, the value is updated with the value from the argument dictionary.

For example:

info = {"name": "John", "age": 30, "city": "New York"}
info.update({"age": 31, "country": "United States"})
print(info)    # prints {"name": "John", "age": 31, "city": "New York", "country": "United States"}

How do you delete an item from a dictionary in Python?

To delete an item from a dictionary in Python, you can use the “del” statement and the key of the item you want to delete. For example:

info = {"name": "John", "age": 30, "city": "New York"}
del info["age"]
print(info)    # prints {"name": "John", "city": "New York"}

You can also use the “pop()” method to delete an item from a dictionary and store its value in a variable. The “pop()” method takes the key of the item as an argument, and removes the item from the dictionary. If the key is not found in the dictionary, the “pop()” method returns a default value specified as an optional argument.

For example:

info = {"name": "John", "age": 30, "city": "New York"}
age = info.pop("age", 0)
print(info)    # prints {"name": "John", "city": "New York"}
print(age)     # prints 30

How do you iterate over the items in a dictionary in Python?

To iterate over the items in a dictionary in Python, you can use a “for” loop and the “items()” method. The “items()” method returns a tuple of the key-value pairs in the dictionary, which you can unpack into variables in the “for” loop.

For example:

info = {"name": "John", "age": 30, "city": "New York"}

for key, value in info.items():
    print(key, ":", value)

The output of this code will be:

name : John
age : 30
city : New York

Alternatively, you can use the “keys()” method to iterate over the keys in the dictionary, or the “values()” method to iterate over the values.

info = {"name": "John", "age": 30, "city": "New York"}

# iterating over keys
for key in info.keys():
    print(key)

# iterating over values
for value in info.values():
    print(value)