Python dictionary append with examples

By: On:

In this Python tutorial, we will learn about the Python dictionary append. Also, we will discuss:

  • Python dictionary append
  • Python append to the lists of each key inside a dictionary
  • Convert list to dictionary python

If you are new to Python check out the below python tutorials:

Python dictionary append

Python dictionary append is used to add the element in the existing dictionary. In the dictionary, append can be done by list as a list have this feature.

Example:

food = {'Nishant': 'Burger'}
food['Nishant'] = [4]
food['Nishant'].append('Pizza')
print(food)

After writing the above code (python dictionary append), Ones you will print “food” then the output will appear as a “ {‘Nishant’: [4, ‘pizza’]} ”. Here, the append is used to add the elements in the dictionary. You can refer to the below screenshot for creating a python dictionary append.

Python dictionary append
Python dictionary append

Python append to lists of each key inside a dictionary

By using ” + ” operator we can append the lists of each key inside a dictionary in Python.


Example:

myvalue = {'Akash': [], 'Bharat': []}
myvalue['Akash'] += [1,3]
myvalue['Bharat'] += [4,6]
print(myvalue)

After writing the above code (Python append to the lists of each key inside a dictionary), Ones you will print “myvalue” then the output will appear as a “ {‘Akash’: [1, 3], ‘Bharat’: [4, 6]} ”. Here, the ” + “ operator is used to append in the dictionary. You can refer to the below screenshot for creating a python append to the lists of each key inside a dictionary.

Python append to the lists of each key inside a dictionary

Convert list to dictionary python

In Python, to convert a list to dictionary we can use dict.fromkey() method to convert the given list into dictionary.

Example:

name = ['Tom', 'Mack', 'Jim']
dictionary = dict.fromkeys(name, 'pass')
print(dictionary)

After writing the above code (convert list to dictionary python), Ones you will print “ dictionary ” then the output will appear as a “ {‘Tom’: ‘pass’, ‘Mack’: ‘pass’, ‘Jim’: ‘pass’} ”. Here, dict.fromkeys() will convert the list into a dictionary with the specified value “pass”. You can refer to the below screenshot for convert list to dictionary python.

Convert list to dictionary python
Convert list to dictionary python

We can also, convert list to dictionary in python by using dictionary comprehension.

Example:

name = ['Tom', 'Mack', 'Jim']
dictionary = {names : 'pass' for names in name}
print(dictionary)

After writing the above code (convert list to dictionary python), Ones you will print “ dictionary ” then the output will appear as a “ {‘Tom’: ‘pass’, ‘Mack’: ‘pass’, ‘Jim’: ‘pass’} ”. Here, dictionary comprehension will convert the list into a dictionary, and each keys value will be the same. You can refer to the below screenshot for convert list to dictionary python.

Convert list to dictionary python

This is how, we can convert list to dictionary python.


You may like following Python tutorials:

In this tutorial, we will discuss how to append an item to Python dictionary. We discussed the Python dictionary append with examples.

Leave a Comment