Get value in dict python

This article describes how to get the value from a dictionary (dict type object) by the key in Python.

  • Get value from dictionary with dict[key] (KeyError for non-existent keys)
  • Use dict.get() to get the default value for non-existent keys

If you want to extract keys by the value, see the following article.

  • Get key from value in dictionary in Python

Get value from dictionary with dict[key] (KeyError for non-existent keys)

In Python, you can get the value from a dictionary by specifying the key like dict[key].

d = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

print(d['key1'])
# val1

In this case, KeyError is raised if the key does not exist.

# print(d['key4'])
# KeyError: 'key4'

Note that it is no problem to specify a non-existent key if you want to add a new element.

d['key4'] = 'val4'
print(d)
# {'key1': 'val1', 'key2': 'val2', 'key3': 'val3', 'key4': 'val4'}

For more information about adding items to the dictionary, see the following article.

  • Merge multiple dictionaries and add items to a dictionary in Python

Use in to check if the key exists in the dictionary.

  • Check if key/value exists in dictionary in Python

Use dict.get() to get the default value for non-existent keys

You can use the get() method of the dictionary (dict) to get any default value without an error if the key does not exist.

  • Built-in Types - dict.get() — Python 3.9.1 documentation

Specify the key as the first argument. The corresponding value is returned if the key exists, and None is returned if the key does not exist.

print(d.get('key1'))
# val1

print(d.get('key5'))
# None

You can specify the default value to be returned when the key does not exist in the second argument.

print(d.get('key5', 'NO KEY'))
# NO KEY

print(d.get('key5', 100))
# 100

The original dictionary itself does not change.

print(d)
# {'key1': 'val1', 'key2': 'val2', 'key3': 'val3', 'key4': 'val4'}


Accessing Items

You can access the items of a dictionary by referring to its key name, inside square brackets:

Example

Get the value of the "model" key:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict["model"]

Try it Yourself »

There is also a method called get() that will give you the same result:

Example

Get the value of the "model" key:

x = thisdict.get("model")

Try it Yourself »


Get Keys

The keys() method will return a list of all the keys in the dictionary.

The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.

Example

Add a new item to the original dictionary, and see that the keys list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.keys()

print(x) #before the change

car["color"] = "white"

print(x) #after the change

Try it Yourself »



Get Values

The values() method will return a list of all the values in the dictionary.

The list of the values is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the values list.

Example

Make a change in the original dictionary, and see that the values list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.values()

print(x) #before the change

car["year"] = 2020

print(x) #after the change

Try it Yourself »

Example

Add a new item to the original dictionary, and see that the values list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.values()

print(x) #before the change

car["color"] = "red"

print(x) #after the change

Try it Yourself »


Get Items

The items() method will return each item in a dictionary, as tuples in a list.

The returned list is a view of the items of the dictionary, meaning that any changes done to the dictionary will be reflected in the items list.

Example

Make a change in the original dictionary, and see that the items list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.items()

print(x) #before the change

car["year"] = 2020

print(x) #after the change

Try it Yourself »

Example

Add a new item to the original dictionary, and see that the items list gets updated as well:

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.items()

print(x) #before the change

car["color"] = "red"

print(x) #after the change

Try it Yourself »


Check if Key Exists

To determine if a specified key is present in a dictionary use the in keyword:

Example

Check if "model" is present in the dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
if "model" in thisdict:
  print("Yes, 'model' is one of the keys in the thisdict dictionary")

Try it Yourself »



How do you get a specific value in a dictionary Python?

In Python, you can get the value from a dictionary by specifying the key like dict[key] . In this case, KeyError is raised if the key does not exist. Note that it is no problem to specify a non-existent key if you want to add a new element.

How do you select a specific value from a dictionary?

To access a specific value in a dictionary, you can use the dictionary name, followed by the specific key in square brackets.

Does dict values () return a list?

The methods dict. keys() and dict. values() return lists of the keys or values explicitly.

How do you get a specific key from a dictionary Python?

Method 1 : Using List. Step 1: Convert dictionary keys and values into lists. Step 2: Find the matching index from value list. Step 3: Use the index to find the appropriate key from key list.