Skip to main content
Practice

Efficiently Handling Dictionary Data

In Python, a method refers to a function that is available for a specific data type.

For example, the append method for lists, which we covered in a previous lesson, adds a new value to a list.

Example of using the append method
my_list = [1, 2, 3]

# Adding 4 to the list
my_list.append(4)

# Prints [1, 2, 3, 4]
print(my_list)

Similarly, there are various methods that can be used to handle dictionaries effectively.


Safely Retrieve Values with get

To retrieve a value for a specific key in a dictionary, you typically use the ["key"] notation.

However, if the key does not exist in the dictionary, it will raise an error.

This is where the get method becomes useful.

Example of using the get method
my_dict = {'name': 'Alice', 'age': 25}

# The key 'name' exists, so the corresponding value is returned.
name = my_dict.get('name')

# Prints 'Alice'
print(name)

# The key 'address' does not exist, so None is returned.
address = my_dict.get('address')

# Prints None
print(address)

# If a default value is set, it is returned when the key is not found.
address = my_dict.get('address', 'Hello')

# Prints 'Hello'
print(address)

By using the get method, you can safely retrieve values from a dictionary, returning a default value if the key is not present, thus improving code stability.


Viewing Dictionary Keys with keys

If you need to check which keys are stored in a dictionary, the keys method can be used to retrieve all keys.

Example of using the keys method
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}

# Retrieves all keys as a list.
keys = my_dict.keys()
# dict_keys(['name', 'age', 'city'])

Viewing All Values at Once with values

To view all values stored in a dictionary, you can use the values method.

This method returns a list of all the values in the dictionary.

Example of using the values method
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}

# Retrieves all values as a list.
values = my_dict.values() # dict_values(['Alice', 25, 'New York'])

Other Useful Methods to Know

There are other methods that are beneficial to be aware of when working with dictionaries.

  • items: Returns key-value pairs as tuples.

  • update: Updates the dictionary with another dictionary or key-value pairs.

  • pop: Removes and returns the value for a specified key.

Example of using other useful methods
my_dict = {'name': 'Alice', 'age': 25}

# Using the 'items' method to view key-value pairs.
items = my_dict.items()
# dict_items([('name', 'Alice'), ('age', 25)])


# Using the 'update' method to add or update values.
my_dict.update({'age': 26, 'city': 'New York'})
# {'name': 'Alice', 'age': 26, 'city': 'New York'}


# Using the 'pop' method to remove the 'name' key and return its value.
name = my_dict.pop('name')
# Returns 'Alice', resulting in {'age': 26, 'city': 'New York'}

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.