Skip to content

How to add an element to a dictionary in Python ( in 3 ways )

How to add an element to a dictionary in Python

In this tutorial we will learn how to add an element to a dictionary in Python . We will do it using three ways . They are as follows –

  • Using dictionary key
  • Python dictionary update() method
  • Python dictionary setdefault() method

Let us have a look about all this ways with examples –

Add an item to a dictionary in Python using dictionary key

We can add an item to a dictionary using dictionary key . If the key is found in the dictionary then the value of the key will be updated with the new value otherwise a new item will be added to the dictionary as specified key:value pair .

Syntax

Dictionary_name[key]=value

Example 1: Add an item to a dictionary in Python

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# adding a new item to the dictionary
marks["english"]=83

# printing the dictionary
print(marks)
{'math': 90, 'physics': 80, 'chemistry': 88, 'english': 83}
  • In the above code first we have declared a dictionary named marks as {"math":90, "physics":80, "chemistry":88}
  • Then we have added a new item "english":83 to the dictionary using dictionary key .
  • After that we have printed the dictionary . From the output we can see that "english":83 is added to the dictionary .

Example 2: Update an item of a dictionary in Python

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# Updating value of math
marks["math"]=95

# printing the dictionary
print(marks)
{'math': 95, 'physics': 80, 'chemistry': 88}
  • In the above code we want to add a new item to the dictionary .
  • As the key is found in the dictionary so instead of adding a new item to the dictionary it has updated the value of the given key in the dictionary .
  • From the output we can see that value of math is changed to 95 .

Python Dictionary update() method

It adds new items to the dictionary . During addition if the the key is found in the given dictionary then the value of the existing key will be updated otherwise new items will be added in the dictionary.

Syntax of update() method

dictionary.update(mapping)

Parameters of update() method

  • mapping = It is a dictionary or an iterable object that can be converted to dictionary . .

Return type of update() method

It returns None .

Examples of Python Dictionary update() method

Example 1: Add an item to a dictionary in Python

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# adding a new item to the dictionary
marks.update({"english":83})

# printing the dictionary
print(marks)
{'math': 90, 'physics': 80, 'chemistry': 88, 'english': 83}
  • In the above code first we have declared a dictionary named marks as {"math":90, "physics":80, "chemistry":88}
  • Then we have added a new item "english":83 to the dictionary using dictionary update() method .
  • After that we have printed the dictionary . From the output we can see that "english":83 is added to the dictionary .

Example 2: Update an item of a dictionary in Python

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# Updating value of math
marks.update({"math":95})

# printing the dictionary
print(marks)
{'math': 95, 'physics': 80, 'chemistry': 88}
  • In the above code we want to add a new item to the dictionary .
  • As the key is found in the dictionary so instead of adding a new item to the dictionary it has updated the value of the given key in the dictionary .
  • From the output we can see that value of math is changed to 95 .

Example 3: Add multiple items to a dictionary in Python

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# adding a new item to the dictionary
marks.update([("english",83),("math",95)])

# printing the dictionary
print(marks)

# adding another item to the dictionary
marks.update(math=92,biology=81)

# printing the dictionary
print(marks)
{'math': 95, 'physics': 80, 'chemistry': 88, 'english': 83}
{'math': 92, 'physics': 80, 'chemistry': 88, 'english': 83, 'biology': 81}
  • In the above code at first a new key "english" is added to the dictionary with value 83 and value of "math" key is changed to 95 .
  • Second , value of "math" key is changed again to 92 and a new key "biology" is added to the dictionary with value 81 .

Python Dictionary setdefault() method

It returns the value of the specified key if found in the dictionary . If the specified key is not found in the dictionary then it will return the value given in the 2nd argument of the setdefault() method and add a new item to the dictionary with the specified key and the given value .

Syntax of setdefault() method

dictionary.setdefault(key,[value])

Parameters of setdefault() method

  • key = It is the key of the dictionary whose value will be returned .
  • value = Optional . It is the value of the specified key if the key is not found in the dictionary . Default is None .

Return type of setdefault() method

It returns the value of the specified key if found in the dictionary . If the specified key is not found in the dictionary then it will return the value given in the 2nd argument of the setdefault() method otherwise None .

Examples of Python Dictionary setdefault() method

Example 1: Add an item to a dictionary in Python

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# adding a new item to the dictionary
value = marks.setdefault("english",83)

# printing the dictionary
print(marks)

# printing the value
print(value)
{'math': 90, 'physics': 80, 'chemistry': 88, 'english': 83}
83
  • In the above code first we have declared a dictionary named marks as {"math":90, "physics":80, "chemistry":88}
  • Then we have added a new item "english":83 to the dictionary using dictionary setdefault() method and stored the returned value to value variable .
  • After that we have printed the dictionary and the value variable . From the output we can see that "english":83 is added to the dictionary and value is 83 .

Example 2: Get the value of a dictionary key in Python

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# getting the value of math kjey
value = marks.setdefault("math",95)

# printing the dictionary
print(marks)

# printing the value
print(value)
{'math': 90, 'physics': 80, 'chemistry': 88}
90
  • In the above code we want to add a new item to the dictionary .
  • As the key is found in the dictionary so instead of doing anything it just returned value of the specified key . We have stored the value to a variable named value .
  • From the output we can see that value of math is changed to 90 .

Example 3: Get the value of a dictionary key without using value parameter

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# getting the value of math key
value = marks.setdefault("math")

# printing the dictionary
print(marks)

# printing the value
print(value)
{'math': 90, 'physics': 80, 'chemistry': 88}
90

Example 4: Add an item to a dictionary without using value parameter

# declaring a dictionary
marks={"math":90,"physics":80,"chemistry":88}

# adding a new item to the dictionary
value = marks.setdefault("english")

# printing the dictionary
print(marks)

# printing the value
print(value)
{'math': 90, 'physics': 80, 'chemistry': 88, 'english': None}
None

In the above code as nothing is given in the value parameter and key is not found in the dictionary so a new item is added to the dictionary with the specified key and value as None .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply