Python dictionary append

In this tutorial, we will see how to append items to dictionary.
There is no method called append in dictionary in python, you can actually use update method to add key value pairs to the dictionary.

Simplest way to add item to dictionary

Output:

{1: ‘one’, 2: ‘two’, 3: ‘three’}
{1: ‘one’, 2: ‘two’, 3: ‘three’, 4: ‘four’}

You can use dictionary’s update method to append dictionary or iterable to the dictionary.
Let’s understand with the help of example.

Output:

{1: ‘one’, 2: ‘two’, 3: ‘three’}
{1: ‘one’, 2: ‘two’, 3: ‘three’, 4: ‘four’, 5: ‘five’, 6: ‘six’}
{1: ‘one’, 2: ‘two’, 3: ‘three’, 4: ‘four’, 5: ‘five’, 6: ‘six’, ‘a’: 1, ‘b’: 2, ‘c’: 3}

That’s all about appending items to dictionary in python.

Was this post helpful?

Leave a Reply

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