Python – Update values of a list of dictionaries

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']}]

Time Complexity: O(1)
Auxiliary Space: O(1)

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 fourth 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']}]

Time complexity: O(1)
Auxiliary space: O(1)

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 fourth 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']}]

The time complexity of updating the subjects of each student is O(1), since we are only inserting and popping elements from a list which takes constant time.

The space complexity of the program is also O(1), since we are not using any additional space other than the given list of dictionaries data.