note.nkmk.me

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

Posted: 2021-09-12 / Tags: Python, Dictionary

In Python, you can add a new item to the dictionary dict with dict_object[key] = new_value. In this way, if the key already exists, the value is updated (overwritten) with the new value.

By using the setdefault() method, you can add items with new values only for new keys, without changing the values for existing keys.

This is useful when you don't want to change an item that already exists.

This article describes the following contents.

  • Add and update an item to the dictionary by specifying the key
  • How to use the setdefault() method
  • Return value of the setdefault() method

in is used to test whether the key exists in dict or not. See the following article.

Sponsored Link

Add and update an item to the dictionary by specifying the key

You can add/update dictionary items in the following way.

dict_object[key] = new_value

If a non-existent key is specified, a new item is added. If a key that already exists is specified, the existing value is updated (overwritten).

d = {'k1': 1, 'k2': 2}

d['k3'] = 3
print(d)
# {'k1': 1, 'k2': 2, 'k3': 3}

d['k1'] = 100
print(d)
# {'k1': 100, 'k2': 2, 'k3': 3}

For more information on how to add multiple items at once, or merge multiple dictionaries, see the following articles.

How to use the setdefault() method

In the setdefault() method, the first argument is the key, and the second argument is the value.

If the key specified in the first argument does not exist, a new item is added.

d = {'k1': 1, 'k2': 2}

d.setdefault('k3', 3)
print(d)
# {'k1': 1, 'k2': 2, 'k3': 3}

The default value of the second argument is None. If it is omitted, the item with the value None is added.

d.setdefault('k4')
print(d)
# {'k1': 1, 'k2': 2, 'k3': 3, 'k4': None}

If the key specified as the first argument already exists, the existing item remains unchanged as the original, no matter what value is specified as the second argument.

d.setdefault('k1', 100)
print(d)
# {'k1': 1, 'k2': 2, 'k3': 3, 'k4': None}
Sponsored Link

Return value of the setdefault() method

The setdefault() method returns the value for the key specified as the first argument.

If the key specified in the first argument does not exist, the item is added with the value specified in the second argument, and then the value is returned.

print(d.setdefault('k5', 5))
# 5

print(d)
# {'k1': 1, 'k2': 2, 'k3': 3, 'k4': None, 'k5': 5}

If the second argument is omitted, the item whose value is None is added and None is returned.

print(d.setdefault('k6'))
# None

print(d)
# {'k1': 1, 'k2': 2, 'k3': 3, 'k4': None, 'k5': 5, 'k6': None}

If the key specified in the first argument already exists, the value for that key is returned as is.

print(d.setdefault('k1', 100))
# 1

print(d.setdefault('k1', -100))
# 1

print(d.setdefault('k1'))
# 1

print(d)
# {'k1': 1, 'k2': 2, 'k3': 3, 'k4': None, 'k5': 5, 'k6': None}
Sponsored Link
Share

Related Categories

Related Articles