Skip to content

How to Add Items to a Python Dictionary Vijetha Spark By {Examples}

  • by

Python provides various ways to add items or elements to a dictionary. Dictionaries are mutable, so we can add, delete, and update the dictionaries. In dictionaries, there are no in-built methods for adding elements but by using various Python methods and operators we can add items to the dictionary very easily.

A Python dictionary is a collection that is unordered, mutable, and does not allow duplicates. Each element in the dictionary is in the form of key:value pairs. Dictionary elements should be enclosed with {} and key: value pair separated by commas. The dictionaries are indexed by keys. In this article, I will explain how to add items to the Dictionary using ‘[]’ operator, update() method, merge ‘|’, ‘**’, and, for loop with enumerate() methods with examples.

1. Quick Examples of How to Remove Elements from Dictionary

Following are quick examples of how to remove element from Python dictionary.

# Below are the quick examples

# Create a dictionary
my_dict = {‘course’:’python’,’fee’:4000}
print(“Original dictionary:”, my_dict)

# Example 1: Add element to a dictionary using ‘[]’ operator
my_dict[‘tutor’]=’Richerd’
print(“New added dictionary:”, my_dict)

# Example 2: Update the dictionary by adding
# multiple items to a dictionary
my_dict.update({‘tutor’:’Richerd’, ‘duration’:’45 days’})

# Example 3: Overwrites the values of existing keys
# with new values using update()
my_dict.update({‘fee’:5000, ‘duration’:’45 days’})
print(“Updated dictionary:”, my_dict)

# Example 4: Add items to dictionary by merging two dictionaries
my_dict1 = {‘course’:’python’,’fee’:4000}
my_dict2 = {‘tutor’:’Richerd’, ‘duration’:’45 days’}
new_dict = my_dict1 | my_dict2
print(“New added dictionary:n”, new_dict)

# Example 5: using __setitem__ method add item to dict
my_dict.__setitem__(‘duration’, ’45 days’)

# Example 6: Using ** operator add item to dict
new_dict = {**my_dict, **{‘duration’: ’45 days’}}

# Example 7: Using for loop enumerate() to add item to dict
for i, val in enumerate(list):
my_dict1[i] = val
print(my_dict1)

# Example 8: Using zip() method to add multiple
# items to dict
dict = {}
keys = [‘course’, ‘fee’, ‘tutor’]
values = [‘python’, 4000, ‘Richerd’]
for key, value in zip(keys, values):
dict[key] = value

2. Using [] Operator to Add Item to Python Dictionary

Using the ‘[]’ operator we can add the item to the dictionary by assigning a value to the corresponding key. If the key doesn’t exist, the new key will be added to the dictionary. If the key exists, it will be overwritten with a new key.

# Create a dictionary
my_dict = {‘course’:’python’,’fee’:4000}
print(“Original dictionary:”, my_dict)

# Add element to a dictionary using ‘[]’ operator
my_dict[‘tutor’]=’Richerd’
print(my_dict)

# Add element to a dictionary
my_dict[‘duration’]=’45 days’
print(my_dict)
print(“New added dictionary:”, my_dict)

3. Add Item to Python Dictionary using update() Method 

When we want to add multiple items to a dictionary at once Python provides an in-built update() method. The update() method allows an iterable sequence of key/value pairs as an argument and adds its key-value pairs to the original dictionary. The update() method updates the values of existing keys if they are already present otherwise it adds new elements.

# Create dictionary
my_dict = {‘course’:’python’,’fee’:4000}
print(“Original dictionary:”, my_dict)

# Update the dictionary by adding
# multiple items to a dictionary
my_dict.update({‘tutor’:’Richerd’, ‘duration’:’45 days’})
print(“Updated dictionary:”, my_dict)

Yields below output.

Using update() method we can update the values of existing keys with the new values. Let’s take an example,

# Create dictionary
my_dict = {‘course’:’python’,’fee’:4000}
print(“Original dictionary:”, my_dict)

# Overwrites the values of existing keys
# with new values using update()
my_dict.update({‘fee’:5000, ‘duration’:’45 days’})
print(“Updated dictionary:”, my_dict)

# Output:
# Original dictionary: {‘course’: ‘python’, ‘fee’: 4000}
# Updated dictionary: {‘course’: ‘python’, ‘fee’: 5000, ‘duration’: ’45 days’}

4. Add Items to Dictionary Using the Merge ‘|‘ Operator

Use the merge ‘|’ operator to merge given two dictionaries and create a new dictionary. Take two dictionaries and merge them using the merge ‘|’ operator.

# Add items to dictionary by merging two dictionaries
my_dict1 = {‘course’:’python’,’fee’:4000}
my_dict2 = {‘tutor’:’Richerd’, ‘duration’:’45 days’}

new_dict = my_dict1 | my_dict2
print(“New added dictionary:n”, new_dict)

# Output:
# New added dictionary:
# {‘course’: ‘python’, ‘fee’: 4000, ‘tutor’: ‘Richerd’, ‘duration’: ’45 days’}

5. Using the __setitem__() Method

The __setitem__ method can also be used to add a key-value pair to a dictionary. Pass the (key, value) pair into this function to add it.

# using __setitem__ method add item to dict
my_dict.__setitem__(‘duration’, ’45 days’)
print(“new dictionary:n”, my_dict)

# Output:
# new dictionary:
# {‘course’: ‘python’, ‘fee’: 4000, ‘duration’: ’45 days’}

6. Using the ** operator 

Alternatively, We can add items to dict by merging the given dictionary and key/value pair(which we want to add to dictionary) in another dictionary Using ** operator. It will add the key/value pair to given dictionary.

# Using ** operator add item to dict
new_dict = {**my_dict, **{‘duration’: ’45 days’}}
print(“New dictionaryn:”, new_dict)

# Output:
# New dictionary:
# {‘course’: ‘python’, ‘fee’: 4000, ‘duration’: ’45 days’}

7. Add Item to Dict using a for loop and enumerate() method

Using the enumerate() method we can iterate the given list, and then add each item to the dictionary by using its index as a key for each value.

my_dict = {‘course’:’python’,’fee’:4000}
list = {‘tutor:Richerd’, ‘duration:45 days’}

# Using for loop enumerate() to add item to dict
for i, val in enumerate(list):
my_dict1[i] = val
print(my_dict1)

# Output:{‘course’: ‘python’, ‘fee’: 4000, 0: ‘duration:45 days’, 1: ‘tutor:Richerd’}

8. Add Multiple Items to a Python Dictionary Zip()

using the zip() method of python we can add keys and values to an empty dict. Using this method we can also update the existing dictionary by adding keys and values instead of using empty dict.

# Using zip() method to add multiple
# items to dict
dict = {}
keys = [‘course’, ‘fee’, ‘tutor’]
values = [‘python’, 4000, ‘Richerd’]
for key, value in zip(keys, values):
dict[key] = value
print(dict)

# Output: {‘course’: ‘python’, ‘fee’: 4000, ‘tutor’: ‘Richerd’}

9. Conclusion

In this article, I have explained how to add items to a dictionary using various methods of Python. And also explained using various methods and operators how we can update the dictionary by adding single element or multiple elements to dict.

Happy Learning!!

 Python provides various ways to add items or elements to a dictionary. Dictionaries are mutable, so we can add, delete, and update the dictionaries. In dictionaries, there are no in-built methods for adding elements but by using various Python methods and operators we can add items to the dictionary very easily. A Python dictionary is  Read More Python, Python Tutorial, Python Dictionary Examples 

Leave a Reply

Your email address will not be published. Required fields are marked *