Getting Values from a Dictionary in Python

To access all values in a dictionary in Python, call the values() method.

For example:

dict.values()

Or if you want to get a single value, use the square bracket [] access operator:

dict['key']

In case you’re unsure whether the value exists, it makes more sense to use the dict.get() method to access the values. This prevents the program from crashing if the value doesn’t exist.

dict.get('key')

This is the quick answer. But let’s take a more detailed look at accessing dictionary values in Python.

How to Get All Dictionary Values in Python

If you want to get all the values of a dictionary, use the values() method.

For example:

player_data = {"Ronaldo": 7, "Messi": 10, "Bale": 9}
values = player_data.values()

This returns a dict_keys object. But you can convert it to a list using the list() function:

values = list(values)

Now, this list contains all the values from the dictionary (the player numbers):

print(values)

Output:

[7, 10, 9]

How to Get a Dictionary Value in Python

Two main ways to get a dictionary value by key in Python

There are two ways to access a single value of a dictionary:

  1. The square brackets [] approach.
  2. The safer get() method.

1. Get a Dictionary Value with Square Brackets

Accessing a dictionary value with square brackets in Python

To get a single value from a dictionary, use the square brackets approach.

To do this, place the key whose value you are looking for inside square brackets after the dictionary:

dict[key]

For example:

player_data = {"Ronaldo": 7, "Messi": 10, "Bale": 9}

ronaldo_number = player_data["Ronaldo"]

print(ronaldo_number)

Output:

7

The problem with this approach is if there is no such key-value pair in the dictionary, an error is thrown and your program crashes.

For instance, let’s try to get a number of a player that does not exist:

player_data = {"Ronaldo": 7, "Messi": 10, "Bale": 9}
rivaldo_number = player_data.get("Rivaldo")

print(rivaldo_number)

Output:

KeyError: 'Rivaldo'

To overcome this issue, use the get() method to get a value from a dictionary instead.

2. Python Dictionary get() Method

Accessing python dictionary values with the get method

In addition to using square brackets to access dictionary values in Python, you can use the get() method.

To do this, enter the key whose value you are searching for inside the get() method.

dict.get(key)

For example:

player_data = {"Ronaldo": 7, "Messi": 10, "Bale": 9}
ronaldo_number = player_data.get("Ronaldo")

print(ronaldo_number)

Output:

7

Now if you try to access a non-existent value from the dictionary, you get a None back. This is better as your program does not crash even the value you are looking for is not there:

player_data = {"Ronaldo": 7, "Messi": 10, "Bale": 9}
rivaldo_number = player_data.get("Rivaldo")

print(rivaldo_number)

Output:

None

Conclusion

To access a dictionary value in Python you have three options:

  1. Use the dictionary.values() method to get all the values
  2. Use the square brackets [] to get a single value (unsafely).
  3. Use the dictionary.get() method to safely get a single value from a dictionary.

Using get() is safe because it returns None if the value is not found. Using square brackets crashes your program if the value is not found.

Thanks for reading. I hope you found the values you were looking for. Happy coding!

Further Reading