Getting Values from a Dictionary in Python

To access a value in 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']

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

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

  • The “traditional” square brackets approach.
  • The safer get() method.

Get a Dictionary Value with Square Brackets

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

Enter the key whose value you are looking for into the square brackets.

For instance:

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 value, an error is thrown and your program crashes.

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

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

print(rivaldo_number)

Output:

KeyError: 'Rivaldo'

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

Python Dictionary get() Method

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

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

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:

  • Use the dictionary.values() method to get all the values
  • Use the square brackets [] to get a single value (unsafely).
  • 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

50 Python Interview Questions with Answers

50+ Buzzwords of Web Development

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published.