6 Ways to Add Values to Dictionary in Python

Here are six ways to add values to a dictionary in Python:

  1. Direct assignment
  2. Using dict.update() Method
  3. Using merge operator (|)
  4. Using update operator (|=)
  5. Using a for loop
  6. 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

Visual Representation of add values to dictionary in python using assignment operator(=)

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?

Visual Representation of 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

Visual Representation of Using dict.update() Method

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

Visual Representation of Using merge operator

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

Visual Representation of Using update operator

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.

1 thought on “6 Ways to Add Values to Dictionary in Python”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.