How to Get Values from a Dictionary in Python
Table of Contents
A dictionary in Python is very useful because it allows you to store data in a key-value pair.
After you define a dictionary and add values to it, you will need to later on access them.
In this post, we'll learn how to access the values of a dictionary in Python.
How to get All Values from a Dictionary in Python
To start, let's first define a dictionary:
PYTHONdictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
print(dictionary)
BASH{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
If you want to access all the values in a dictionary, you can use the values()
method.
This will return a dictionary view object that contains all the values in the dictionary.
PYTHONdictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
values = dictionary.values()
print(values)
BASHdict_values(['value1', 'value2', 'value3'])
You can convert this dictionary view object to a list by using the list()
function.
PYTHONdictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
values = dictionary.values()
values_list = list(values)
print(values_list)
BASH['value1', 'value2', 'value3']
How to get a specific Value from a Dictionary using the get() method
If you want to access a specific value in a dictionary, you can use the get()
method.
This method takes in a key as a parameter and returns the value associated with that key.
PYTHONdictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
value = dictionary.get("key1")
print(value)
BASHvalue1
If the key you pass in does not exist in the dictionary, the get()
method will return None
.
PYTHONdictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
value = dictionary.get("key4")
print(value)
BASHNone
How to get a specific Value from a Dictionary using the [] operator
Another way to access a specific value in a dictionary is by using the []
operator.
This operator takes in the key you want to get the value for:
PYTHONdictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
value = dictionary["key1"]
print(value)
BASHvalue1
If the key you pass in does not exist in the dictionary, you will get a KeyError
.
PYTHONdictionary = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
value = dictionary["key4"]
print(value)
BASHKeyError: 'key4'
Conclusion
In this post, we learned how to access the values of a dictionary in Python.
If you want to get all the values in a dictionary, you can use the values()
method.
If you want to get a specific value in a dictionary, you can use the get()
method or the []
operator.
Thanks for reading!
- Getting Started with Solid
- Getting Started with Svelte
- Create an RSS Reader in Node
- Getting Started with Electron
- How to Set Up Cron Jobs in Linux
- How to deploy a .NET app using Docker
- Best Visual Studio Code Extensions for 2022
- How to build a Discord bot using TypeScript
- Getting Started with Sass
- Learn how to use v-model with a custom Vue component
- Learn how to build a Slack Bot using Node.js
- Getting Started with Vuex: Managing State in Vue