How to add new keys to a dictionary in python?

A dictionary is a collection of key-value pairs that are ordered, mutable, and doesn't allow redundant data. Dictionaries are written with curly brackets {}. In Python 3.6 and earlier dictionaries are unordered.

Let us consider a dictionary num_dict. 1, 2, 3 are keys, and one, two, three are respective values of the keys.

num_dict = {1: "one", 2: "two", 3: "three" }

This article discusses various ways to add a key-value pair in a dictionary.

Adding a key-value pair using a new index key

Consider a dictionary color_dict = {1 : "red", 2 : "black", 3 : "green"}. A new item is added to the dictionary by using a new index key and assigning a value to it.

color_dict = {1: "red", 2: "black", 3: "green"}
print("Color_dict = ", color_dict )
color_dict[4] = "white"
print("Updated dictionary: ", color_dict)

The output of the updated dictionary.

Color_dict =  {1: 'red', 2: 'black', 3: 'green'}
Updated dictionary:  {1: 'red', 2: 'black', 3: 'green', 4: 'white'}

Adding a key-value pair using the update() method

update() method adds the item to the dictionary from the given arguments. If the keys already existing in a dictionary it skips those key-value pairs.

Consider a dictionary color_dict = {1 : "red", 2 : "black", 3 : "green"}. color_dict.update({1:"red", 4: "orange"}) will add {4 : "orange"} to the color_dict and will omit the {1 : "red"} key-value pair.

color_dict = {1: "red", 2: "black", 3: "green"}
print("Color_dict = ", color_dict )
color_dict.update({1: "red", 4: "orange"}) 
print("Updated dictionary: ", color_dict)

The output of the updated dictionary

Color_dict =  {1: 'red', 2: 'black', 3: 'green'}
Updated dictionary:  {1: 'red', 2: 'black', 3: 'green', 4: 'orange'}

Adding a key-value pair using ** operator

The ** operator is followed by the dictionary variable unpacking all the key-value pairs of a dictionary. Any number of key-value pairs can be added by using this method. This method merges the old dictionary with new key-value pairs in another dictionary.

Consider a dictionary color_dict = {1 : "red", 2 : "black", 3 : "green"}. **color_dict unpacked all the items of the dictionary and merge it with another dictionary {4 : "orange"}.

color_dict = {1: "red", 2: "black", 3: "green"}
print("Color_dict = ", color_dict )
new_dict = { **color_dict , **{4: "orange"}}
print("Updated dictionary: ", new_dict)

The output of the updated dictionary

Color_dict =  {1: 'red', 2: 'black', 3: 'green'}
Updated dictionary:  {1: 'red', 2: 'black', 3: 'green', 4: 'orange'}

Adding a key-value pair using __setitem__ method

__setitem__ method is a special method to assign a value to an item. Consider a dictionary color_dict = {1 : "red", 2 : "black", 3 : "green"}. color_dict.__setitem__(4, "white") adds the key-value pair (4, "white") to the color_dict.

color_dict = {1 : "red", 2 : "black", 3 : "green"}
print("Color_dict = ", color_dict )
color_dict.__setitem__(4, "white")
print("Updated dictionary: ", color_dict)

The output of the updated dictionary

Color_dict =  {1: 'red', 2: 'black', 3: 'green'}
Updated dictionary:  {1: 'red', 2: 'black', 3: 'green', 4: 'white'}

Using the __setitem__ method should be avoided because it is computationally inefficient.

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions