Here are six ways to add values to a dictionary in Python:
- Direct assignment
- Using dict.update() Method
- Using merge operator (|)
- Using update operator (|=)
- Using a for loop
- Using setdefault() Method
Method 1: Direct Assignment
You can assign a value to a new or existing key directly using the assignment operator (=).
Visual Representation
Example
my_dict = {}
print("Before Adding Values:", my_dict)
my_dict['Ben 10'] = 1
my_dict['Dexter'] = 2
my_dict['Woody'] = 3
my_dict['Popeye'] = 4
print("After Adding Values:", my_dict)
Output
Before Adding Values: {}
After Adding Values: {'Ben 10': 1, 'Dexter': 2, 'Woody': 3, 'Popeye': 4}
But what happens if a key is already present in the dictionary?
my_dict['Woody'] = 7
print(my_dict)
Output
{'Ben 10': 1, 'Dexter': 2, 'Woody': 7, 'Popeye': 4}
As you can see, the value of ‘Woody’ has been overwritten.
Method 2: Using dict.update() Method
If you want to add multiple key-value pairs or update existing ones. you can use the update() method.
Visual Representation
Example
my_dict = {'Ben 10': 1, 'Dexter': 2}
print("Before Adding Values:", my_dict)
my_dict.update({'Woody': 3, 'Popeye': 4})
print("After Adding Values:", my_dict)
Output
Before Adding Values: {'Ben 10': 1, 'Dexter': 2}
After Adding Values: {'Ben 10': 1, 'Dexter': 2, 'Woody': 3, 'Popeye': 4}
Method 3: Using merge operator ( | )
In Python 3.9 and later versions, you can use the merge operator (|) to combine two dictionaries into a new one.
This operator is useful for merging dictionaries without modifying the original ones.
If there are overlapping keys, the values from the second dictionary are used in the resulting dictionary.
Visual Representation
Example
my_dict = {'Ben 10': 1, 'Dexter': 2}
print("Before Adding Values:", my_dict)
add_dict = {'Woody': 3, 'Popeye': 4}
new_dict = my_dict | add_dict
print("After Adding Values:", new_dict)
Output
Before Adding Values: {'Ben 10': 1, 'Dexter': 2}
After Adding Values: {'Ben 10': 1, 'Dexter': 2, 'Woody': 3, 'Popeye': 4}
Method 4: Using update operator ( |= )
Introduced in Python 3.9, the update(| =)operator updates the first dictionary by adding key-value pairs from the second dictionary.
If keys already exist, then values from the second dictionary will overwrite them.
Visual Representation
Example
my_dict = {'Ben 10': 1, 'Dexter': 2}
print("Before Adding Values:", my_dict)
add_dict = {'Woody': 3, 'Popeye': 4}
my_dict |= add_dict
print("After Adding Values:", my_dict)
Output
Before Adding Values: {'Ben 10': 1, 'Dexter': 2}
After Adding Values: {'Ben 10': 1, 'Dexter': 2, 'Woody': 3, 'Popeye': 4}
Method 5: Using a for loop
If you need to add values from another iterable, such as a list of tuples or another dictionary, you can use a for loop.
Example
my_dict = {'Ben 10': 1, 'Dexter': 2}
print("Before Adding Values:", my_dict)
add_dict = {'Woody': 3, 'Popeye': 4}
new_entries = [('Woody', 3), ('Popeye', 4)]
for key, value in new_entries:
my_dict[key] = value
print("After Adding Values:", my_dict)
Output
After Adding Values: {'Ben 10': 1, 'Dexter': 2, 'Woody': 3, 'Popeye': 4}
Method 6: Using setdefault() Method
If you want to add multiple keys with default values, you can use the setdefault() method.
Example
my_dict = {'Ben 10': 1, 'Dexter': 2}
print("Before Adding Values:", my_dict)
my_dict.setdefault('Woody', 3)
my_dict.setdefault('Popeye', 4)
print("After Adding Values:", my_dict)
Output
Before Adding Values: {'Ben 10': 1, 'Dexter': 2}
After Adding Values: {'Ben 10': 1, 'Dexter': 2, 'Woody': 3, 'Popeye': 4}
That’s it.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Awesome explanation