Skip to content

Access the dictionary

Accessing Dictionary Items

You can access dictionary items by their keys. If you try to access a key that does not exist, you will get a KeyErrorKeyError exception.

Accessing a Key that Exists

You can access dictionary items by their keys. The following example accesses the key namename of the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1["name"])
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1["name"])

Output:

command
C:\Users\username>python dict.py
John
command
C:\Users\username>python dict.py
John

In this example, we access the key namename of the dictionary dict1dict1. We then print the value of the key to the console. The output shows that the value of the key is JohnJohn.

Accessing a Key using the get() Method

You can access dictionary items by their keys using the get()get() method. If you try to access a key that does not exist, you will get NoneNone. The following example accesses the key namename of the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1.get("name"))
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1.get("name"))

Output:

command
C:\Users\username>python dict.py
John
command
C:\Users\username>python dict.py
John

In this example, we access the key namename of the dictionary dict1dict1. We then print the value of the key to the console. The output shows that the value of the key is JohnJohn.

Accessing a Key that Does Not Exist using the get() Method

You can access dictionary items by their keys using the get()get() method. If you try to access a key that does not exist, you will get NoneNone. The following example accesses a key that does not exist:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1.get("address"))
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1.get("address"))

Output:

command
C:\Users\username>python dict.py
None
command
C:\Users\username>python dict.py
None

In this example, we try to access the key addressaddress of the dictionary dict1dict1. We then print the value of the key to the console. The output shows that we get NoneNone because the key does not exist.

Length of a Dictionary

The length of a dictionary is the number of key-value pairs it contains. You can use the len()len() function to get the length of a dictionary. The following example gets the length of the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(len(dict1))
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(len(dict1))

Output:

command
C:\Users\username>python dict.py
3
command
C:\Users\username>python dict.py
3

In this example, we get the length of the dictionary dict1dict1. We then print the length of the dictionary to the console. The output shows that the length of the dictionary is 33.

Iterating Over a Dictionary

You can iterate over a dictionary using a forfor loop. When you iterate over a dictionary, you get the keys of the dictionary. The following example iterates over the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
for key in dict1:
    print(key + " => " + str(dict1[key]))
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
for key in dict1:
    print(key + " => " + str(dict1[key]))

Output:

command
C:\Users\username>python dict.py
name => John
age => 30
city => New York
command
C:\Users\username>python dict.py
name => John
age => 30
city => New York

In this example, we iterate over the dictionary dict1dict1. We then print the keys and values of the dictionary to the console. The output shows that we get the keys of the dictionary.

Accessing the Dictionary Items using the keys() Method

You can access the dictionary items using the keys()keys() method. The following example accesses the dictionary items using the keys()keys() method:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
for key in dict1.keys():
    print(key + " => " + str(dict1[key]))
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
for key in dict1.keys():
    print(key + " => " + str(dict1[key]))

Output:

command
C:\Users\username>python dict.py
name => John
age => 30
city => New York
command
C:\Users\username>python dict.py
name => John
age => 30
city => New York

In this example, we iterate over the dictionary dict1dict1. We then print the keys and values of the dictionary to the console. The output shows that we get the keys of the dictionary.

Accessing the Dictionary Items using the values() Method

You can access the dictionary items using the values()values() method. The following example accesses the dictionary items using the values()values() method:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
for value in dict1.values():
    print(value)
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
for value in dict1.values():
    print(value)

Output:

command
C:\Users\username>python dict.py
John
30
New York
command
C:\Users\username>python dict.py
John
30
New York

In this example, we iterate over the dictionary dict1dict1. We then print the values of the dictionary to the console. The output shows that we get the values of the dictionary.

Accessing the Dictionary Items using the items() Method

You can access the dictionary items using the items()items() method. The following example accesses the dictionary items using the items()items() method:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
for key, value in dict1.items():
    print(key + " => " + str(value))
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
for key, value in dict1.items():
    print(key + " => " + str(value))

Output:

command
C:\Users\username>python dict.py
name => John
age => 30
city => New York
command
C:\Users\username>python dict.py
name => John
age => 30
city => New York

In this example, we iterate over the dictionary dict1dict1. We then print the keys and values of the dictionary to the console. The output shows that we get the keys and values of the dictionary.

Checking if a Key Exists

You can check if a key exists in a dictionary using the inin operator. The following example checks if the key namename exists in the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if "name" in dict1:
    print("The key 'name' exists in the dictionary")
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if "name" in dict1:
    print("The key 'name' exists in the dictionary")

Output:

command
C:\Users\username>python dict.py
The key 'name' exists in the dictionary
command
C:\Users\username>python dict.py
The key 'name' exists in the dictionary

In this example, we check if the key namename exists in the dictionary dict1dict1. We then print a message to the console. The output shows that the key exists in the dictionary.

Checking if a Key Does Not Exist

You can check if a key does not exist in a dictionary using the not innot in operator. The following example checks if the key addressaddress does not exist in the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if "address" not in dict1:
    print("The key 'address' does not exist in the dictionary")
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if "address" not in dict1:
    print("The key 'address' does not exist in the dictionary")

Output:

command
C:\Users\username>python dict.py
The key 'address' does not exist in the dictionary
command
C:\Users\username>python dict.py
The key 'address' does not exist in the dictionary

In this example, we check if the key addressaddress does not exist in the dictionary dict1dict1. We then print a message to the console. The output shows that the key does not exist in the dictionary.

Checking if a Value Exists

You can check if a value exists in a dictionary using the inin operator. The following example checks if the value JohnJohn exists in the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if "John" in dict1.values():
    print("The value 'John' exists in the dictionary")
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if "John" in dict1.values():
    print("The value 'John' exists in the dictionary")

Output:

command
C:\Users\username>python dict.py
The value 'John' exists in the dictionary
command
C:\Users\username>python dict.py
The value 'John' exists in the dictionary

In this example, we check if the value JohnJohn exists in the dictionary dict1dict1. We then print a message to the console. The output shows that the value exists in the dictionary.

Checking if a Value Does Not Exist

You can check if a value does not exist in a dictionary using the not innot in operator. The following example checks if the value JaneJane does not exist in the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if "Jane" not in dict1.values():
    print("The value 'Jane' does not exist in the dictionary")
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if "Jane" not in dict1.values():
    print("The value 'Jane' does not exist in the dictionary")

Output:

command
C:\Users\username>python dict.py
The value 'Jane' does not exist in the dictionary
command
C:\Users\username>python dict.py
The value 'Jane' does not exist in the dictionary

In this example, we check if the value JaneJane does not exist in the dictionary dict1dict1. We then print a message to the console. The output shows that the value does not exist in the dictionary.

Checking if a Key Exists using the get() Method

You can check if a key exists in a dictionary using the get()get() method. The following example checks if the key namename exists in the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if dict1.get("name"):
    print("The key 'name' exists in the dictionary")
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if dict1.get("name"):
    print("The key 'name' exists in the dictionary")

Output:

command
C:\Users\username>python dict.py
The key 'name' exists in the dictionary
command
C:\Users\username>python dict.py
The key 'name' exists in the dictionary

In this example, we check if the key namename exists in the dictionary dict1dict1. We then print a message to the console. The output shows that the key exists in the dictionary.

Checking if a Key Does Not Exist using the get() Method

You can check if a key does not exist in a dictionary using the get()get() method. The following example checks if the key addressaddress does not exist in the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if not dict1.get("address"):
    print("The key 'address' does not exist in the dictionary")
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
if not dict1.get("address"):
    print("The key 'address' does not exist in the dictionary")

Output:

command
C:\Users\username>python dict.py
The key 'address' does not exist in the dictionary
command
C:\Users\username>python dict.py
The key 'address' does not exist in the dictionary

In this example, we check if the key addressaddress does not exist in the dictionary dict1dict1. We then print a message to the console. The output shows that the key does not exist in the dictionary.

Changing Dictionary Items

You can change the value of a dictionary item by using its key.

Changing a Key that Exists

You can change the value of a dictionary item by using its key. The following example changes the value of the key namename of the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict1["name"] = "Jane"
print(dict1)
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict1["name"] = "Jane"
print(dict1)

Output:

command
C:\Users\username>python dict.py
{'name': 'Jane', 'age': 30, 'city': 'New York'}
command
C:\Users\username>python dict.py
{'name': 'Jane', 'age': 30, 'city': 'New York'}

In this example, we change the value of the key namename of the dictionary dict1dict1. We then print the dictionary to the console. The output shows that the value of the key is changed to JaneJane.

Changing a Key that Does Not Exist

You can change the value of a dictionary item by using its key. The following example changes the value of the key addressaddress of the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict1["address"] = "London"
print(dict1)
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict1["address"] = "London"
print(dict1)

Output:

command
C:\Users\username>python dict.py
{'name': 'John', 'age': 30, 'city': 'New York', 'address': 'London'}
command
C:\Users\username>python dict.py
{'name': 'John', 'age': 30, 'city': 'New York', 'address': 'London'}

In this example, we change the value of the key addressaddress of the dictionary dict1dict1. We then print the dictionary to the console. The output shows that the value of the key is changed to LondonLondon.

Changing a Key using the update() Method

You can change the value of a dictionary item by using its key. The following example changes the value of the key namename of the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict1.update({"name": "Jane"})
print(dict1)
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict1.update({"name": "Jane"})
print(dict1)

Output:

command
C:\Users\username>python dict.py
{'name': 'Jane', 'age': 30, 'city': 'New York'}
command
C:\Users\username>python dict.py
{'name': 'Jane', 'age': 30, 'city': 'New York'}

In this example, we change the value of the key namename of the dictionary dict1dict1. We then print the dictionary to the console. The output shows that the value of the key is changed to JaneJane.

Changing a Key that Does Not Exist using the update() Method

You can change the value of a dictionary item by using its key. The following example changes the value of the key addressaddress of the dictionary dict1dict1:

dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict1.update({"address": "London"})
print(dict1)
dict.py
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict1.update({"address": "London"})
print(dict1)

Output:

command
C:\Users\username>python dict.py
{'name': 'John', 'age': 30, 'city': 'New York', 'address': 'London'}
command
C:\Users\username>python dict.py
{'name': 'John', 'age': 30, 'city': 'New York', 'address': 'London'}

In this example, we change the value of the key addressaddress of the dictionary dict1dict1. We then print the dictionary to the console. The output shows that the value of the key is changed to LondonLondon.

Conclusion

In this tutorial, you have learned how to access the dictionary in Python. You also known as how to access the dictionary in Python using the key. You will also learn how to access the dictionary in Python using the get() method. You change the value of the dictionary in Python. For more tutorials, visit our Python Central Hub.

Was this page helpful?

Let us know how we did