You can use the operator to determine whether a key exists in a dictionary

This article describes how to check if a key, value, or key-value pair exists in a dictionary (dict) in Python.

  • Check if a key exists in a dictionary: in operator
  • Check if a value exists in a dictionary: in operator, values()
  • Check if a key-value pair exists in a dictionary: in operator, print(d['key1']) # val1 0

The values() and print(d['key1']) # val1 0 methods are also used to iterate a dictionary with print(d['key1']) # val1 3 loop. See the following article.

  • Iterate dictionary (key and value) with for loop in Python

Sponsored Link

Check if a key exists in a dictionary: in operator

Using the in operator for a dictionary object itself returns if a key exists, i.e., if a dictionary has/contains a key. Use print(d['key1']) # val1 6 to check if a key does not exist in a dictionary.

d = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'} print('key1' in d) # True print('val1' in d) # False print('key4' not in d) # True

source:

The same is true if you use the print(d['key1']) # val1 7 method instead of the dictionary object itself. In the case of the above example, the same result is returned by print(d['key1']) # val1 8.

The print(d['key1']) # val1 9 method was provided in Python 2, but was removed in Python 3.

To get the value for the key, use # print(d['key4']) # KeyError: 'key4' print(d.get('key4')) # None 0.

print(d['key1']) # val1

source:

# print(d['key4']) # KeyError: 'key4' print(d.get('key4')) # None 0 raises an error when the key does not exist, but the # print(d['key4']) # KeyError: 'key4' print(d.get('key4')) # None 2 method returns a specified value (default is # print(d['key4']) # KeyError: 'key4' print(d.get('key4')) # None 3) if the key does not exist.

  • Get value from dictionary by key with get() in Python

# print(d['key4']) # KeyError: 'key4' print(d.get('key4')) # None

source:

You can also add a new item with # print(d['key4']) # KeyError: 'key4' print(d.get('key4')) # None 4. The value is overwritten for an existing key. If you want to add an item with a new value only for a new key without changing the value for an existing key, use the # print(d['key4']) # KeyError: 'key4' print(d.get('key4')) # None 5 method. See the following article.

  • Add an item only when the key does not exist in dict in Python (setdefault())

Check if a value exists in a dictionary: in operator, values()

To check if a value exists in a dictionary, i.e., if a dictionary has/contains a value, use the in operator and the values() method. Use print(d['key1']) # val1 6 to check if a value does not exist in a dictionary.

print('val1' in d.values()) # True print('val4' not in d.values()) # True

source:

See the following article for how to get the key from the value.

  • Get key from value in dictionary in Python

Sponsored Link

Check if a key-value pair exists in a dictionary: in operator, print(d['key1']) # val1 0

To check if a key-value pair exists in a dictionary, i.e., if a dictionary has/contains a pair, use the in operator and the print(d['key1']) # val1 0 method. Specify a tuple print('val1' in d.values()) # True print('val4' not in d.values()) # True 5. Use print(d['key1']) # val1 6 to check if a pair does not exist in a dictionary.

Chủ đề