How to Add Items to Dictionary in Python?

In this article, we will discuss different ways to add items to a dictionary in Python.

Suppose we have a dictionary,

# Dictionary of strings and ints
word_freq = {   "Hello": 11,
                "at": 34,
                "test": 7,
                "this": 23 }

Now we want to add some items to it. By items, we mean some key-value pairs to it. The content of the dictionary should be like this,

{'Hello': 11,
 'at'   : 34,
 'test' : 7,
 'this' : 23,
 'why'  : 14,
 'the'  : 21,
 'how'  : 81,
 'from' : 16}

There are different ways to add items to a dictionary,

  • Using subscript operator i.e. []
  • Using update() function
  • Using dictionary comprehension

Let’s see them one by one,

Adding items to the dictionary using [] operator

To add a key-value pair to the dictionary, pass the new key name in subscript operator and assign a new value to it i.e.

word_freq['why']    = 14

It will insert the key-value pair into the dictionary. But if the given key already exists in the dictionary, it will overwrite the existing value for that key. This method is better if you want to add just a key-value pair in the dictionary. But if you’re going to add multiple key-value pairs, then you need to repeat the same line for every pair i.e.

word_freq['why']    = 14
word_freq['the']    = 21
word_freq['how']    = 81
word_freq['from']   = 16

It will add multiple items to the dictionary. But if you have a very large number of key-value pairs, it is not practical to use this method.

The complete example is as follows,

# Dictionary of strings and ints
word_freq = {   "Hello": 11,
                "at": 34,
                "test": 7,
                "this": 23 }


# add some items (key-value pairs) to the dictionary
word_freq['why']    = 14
word_freq['the']    = 21
word_freq['how']    = 81
word_freq['from']   = 16

print(word_freq)

Output:

{'Hello': 11,
 'at'   : 34,
 'test' : 7,
 'this' : 23,
 'why'  : 14,
 'the'  : 21,
 'how'  : 81,
 'from' : 16}

Adding items to dictionary using update() function

In Python, the dictionary class provides a function update(). It accepts a sequence of key-value pairs and adds them one by one to the dictionary. If any of the keys already exist in the dictionary, then it will overwrite its value. So, we can use this to add multiple key-value pairs to the dictionary in a single line. For example,

# Dictionary of strings and ints
word_freq = {   "Hello": 11,
                "at": 34,
                "test": 7,
                "this": 23 }

# Add multiple items to dictionary in Python
word_freq.update({  'the' : 21,
                    'how': 81,
                    'from': 16} )

print(word_freq)

Output:

{'Hello': 11,
 'at': 34,
 'test': 7,
 'this': 23,
 'the': 21,
 'how': 81,
 'from': 16}

We added four key-value pairs to the dictionary using the update() function. This method is beneficial if you have keys and values in other data structures like lists and want to add them in a single line. Checkout out the next solution,

Adding items to the dictionary using dictionary comprehension

Suppose we have two lists. One of the lists is of a list of strings, and the other is of integers i.e.

list_of_keys = ['why', 'the', 'how', 'from']
list_of_values = [11, 12, 13, 14]

Both the lists are of the same size. Now we want to add the contents of both the lists as key-value pairs in a dictionary. It means all strings from a list of strings should be used as keys, and corresponding ints from a list of integers can be used as value fields.

For example,

# Dictionary of strings and ints
word_freq = {   "Hello": 11,
                "at": 34,
                "test": 7,
                "this": 23 }


list_of_keys = ['why', 'the', 'how', 'from']
list_of_values = [11, 12, 13, 14]

# Add two lists as key-value pairs in dictionary
word_freq. update( { key: value
                     for key, value in zip(list_of_keys, list_of_values)})

print(word_freq)

Output:

{'Hello': 11,
 'at'   : 34,
 'test' : 7,
 'this' : 23,
 'why'  : 11,
 'the'  : 12,
 'how'  : 13,
 'from' : 14}

We new added new key-value pairs to a dictionary by using the values from two lists in Python.

Summary

Today we learned how to add items to a dictionary in Python.

Advertisements

Leave a Comment

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

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

Scroll to Top