Python 3 – Dictionary
In Python, a dictionary is an unordered collection of key-value pairs. Each pair maps the key to its associated value. It is often used to store and manipulate large data sets.
Creating a Dictionary
To create a dictionary, we can simply enclose a comma-separated list of key-value pairs in curly braces {}. Let’s create a dictionary to store some information about a person:
person = {"name": "John Doe", "age": 30, "occupation": "programmer"}
Here, "name"
, "age"
, and "occupation"
are the keys and "John Doe"
, 30
, and "programmer"
are the values. We can access these values using their respective keys:
print(person["name"]) # Output: John Doe
print(person["age"]) # Output: 30
print(person["occupation"]) # Output: programmer
Adding and Modifying Entries
To add a new key-value pair to a dictionary, we simply assign a new value to a new or existing key:
person["email"] = "johndoe@gmail.com"
Here, we have added a new entry with the key "email"
and the value "johndoe@gmail.com"
. Similarly, to modify an existing entry, we simply assign a new value to its key:
person["age"] = 31
Here, we have updated the value associated with the key "age"
from 30
to 31
.
Removing Entries
To remove an entry from a dictionary, we can use the del
keyword followed by the key of the entry we want to remove:
del person["occupation"]
Here, we have removed the entry with the key "occupation"
from the dictionary.
Looping Through a Dictionary
We can loop through the keys of a dictionary using a for
loop:
for key in person:
print(key)
Here, key
is a variable that takes on the value of each key in the dictionary, one at a time. The output of the code above would be:
name
age
email
To loop through the values of a dictionary, we can use the values()
method:
for value in person.values():
print(value)
Here, value
is a variable that takes on the value of each value in the dictionary, one at a time. The output of the code above would be:
John Doe
31
johndoe@gmail.com
To loop through both the keys and values of a dictionary, we can use the items()
method:
for key, value in person.items():
print(key, value)
Here, key
is a variable that takes on the value of each key in the dictionary, and value
is a variable that takes on the value of each value in the dictionary, one at a time. The output of the code above would be:
name John Doe
age 31
email johndoe@gmail.com
Dictionary Methods
Python dictionaries come with a variety of useful methods that make it easy to work with them. Let’s take a look at some of the most common ones:
copy()
The copy()
method makes a copy of a dictionary:
person_copy = person.copy()
Here, person_copy
is a new dictionary that has the same key-value pairs as person
.
clear()
The clear()
method removes all key-value pairs from a dictionary:
person.clear()
Here, we have removed all entries from the person
dictionary.
get()
The get()
method returns the value associated with a given key, or a default value if the key is not found:
print(person.get("name")) # Output: None
print(person.get("name", "Unknown"))# Output: Unknown
Here, the first call to get()
returns None
because "name"
is not a key in the empty person
dictionary. The second call to get()
returns "Unknown"
because "name"
is not a key in the person
dictionary, but we have provided a default value to return instead.
pop()
The pop()
method removes and returns the value associated with a given key:
age = person.pop("age")
print(age) # Output: 31
Here, we have removed the entry with the key "age"
from the person
dictionary and assigned its value to the variable age
.
Conclusion
Python dictionaries are a powerful data structure that allow you to store and manipulate large data sets with ease. By using key-value pairs to organize your data, you can quickly access and modify specific pieces of information, making your code more efficient and easier to read. With plenty of built-in methods to get you started, dictionaries are an essential tool for any Python programmer.