Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Update values of a list of dictionaries

  • Last Updated : 22 Nov, 2021

In this article, we will update the values of a list of dictionaries.

Method 1: Using append() function

The append function is used to insert a new value in the list of dictionaries, we will use pop() function along with this to eliminate the duplicate data.

Syntax:

  • dictionary[row][‘key’].append(‘value’)
  • dictionary[row][‘key’].pop(position)

Where:

  • dictionary is the input list of dictionaries
  • row is the row that we want to update
  • value is the new value to be updated
  • key is the column to be updated
  • position is the place where the old value is there

Python program to create a list of dictionaries

Python3




# create a list of dictionaries
# with student data
data = [
    {'name': 'sravan', 'subjects': ['java', 'python']},
    {'name': 'bobby', 'subjects': ['c/cpp', 'java']},
    {'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
    {'name': 'rohith', 'subjects': ['php', 'os']},
    {'name': 'gnanesh', 'subjects': ['html', 'sql']}
]
  
# display first student
print(data[0])
  
# display all student
data

Output:

{'name': 'sravan', 'subjects': ['java', 'python']}
[{'name': 'sravan', 'subjects': ['java', 'python']},
 {'name': 'bobby', 'subjects': ['c/cpp', 'java']},
 {'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
 {'name': 'rohith', 'subjects': ['php', 'os']},
 {'name': 'gnanesh', 'subjects': ['html', 'sql']}]

Update values in the above list of dictionaries

Python3




# update first student python subject
# to html
data[0]['subjects'].append('html')
data[0]['subjects'].pop(1)
  
# update third student java subject
# to dbms
data[2]['subjects'].append('dbms')
data[2]['subjects'].pop(1)
  
# update forth student php subject
# to php-mysql
data[3]['subjects'].append('php-mysql')
data[3]['subjects'].pop(0)
  
# display updated list
data

Output:

[{'name': 'sravan', 'subjects': ['java', 'html']},
 {'name': 'bobby', 'subjects': ['c/cpp', 'java']},
 {'name': 'ojsawi', 'subjects': ['iot', 'dbms']},
 {'name': 'rohith', 'subjects': ['os', 'php-mysql']},
 {'name': 'gnanesh', 'subjects': ['html', 'sql']}]

Method 2: Using insert() function

This function is used to insert updated data based on an index.

Syntax:

  • dictionary[row][‘key’].insert(index,’value’)
  • dictionary[row][‘key’].pop(position)

Where,

  • dictionary is the input list of dictionaries
  • row is the row that we want to update
  • value is the new value to be updated
  • index is the position to be updated
  • key is the column to be updated
  • position is the place where the old value is there

Example: Python program to update the values in a list of dictionaries

Python3




# update first student python subject
# to html
data[0]['subjects'].insert(0, 'html')
data[0]['subjects'].pop(1)
  
# update third student java subject
# to dbms
data[2]['subjects'].insert(0, 'dbms')
data[2]['subjects'].pop(1)
  
# update forth student php subject
# to php-mysql
data[3]['subjects'].insert(1, 'php-mysql')
data[3]['subjects'].pop(0)
  
# display updated list
data

Output:

[{'name': 'sravan', 'subjects': ['html', 'python']},
 {'name': 'bobby', 'subjects': ['c/cpp', 'java']},
 {'name': 'ojsawi', 'subjects': ['dbms', 'cloud']},
 {'name': 'rohith', 'subjects': ['php-mysql', 'os']},
 {'name': 'gnanesh', 'subjects': ['html', 'sql']}]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!