Python – Update values of a list of dictionaries
Last Updated :
14 Apr, 2023
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
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' ]}
]
print (data[ 0 ])
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
data[ 0 ][ 'subjects' ].append( 'html' )
data[ 0 ][ 'subjects' ].pop( 1 )
data[ 2 ][ 'subjects' ].append( 'dbms' )
data[ 2 ][ 'subjects' ].pop( 1 )
data[ 3 ][ 'subjects' ].append( 'php-mysql' )
data[ 3 ][ 'subjects' ].pop( 0 )
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
data[ 0 ][ 'subjects' ].insert( 0 , 'html' )
data[ 0 ][ 'subjects' ].pop( 1 )
data[ 2 ][ 'subjects' ].insert( 0 , 'dbms' )
data[ 2 ][ 'subjects' ].pop( 1 )
data[ 3 ][ 'subjects' ].insert( 1 , 'php-mysql' )
data[ 3 ][ 'subjects' ].pop( 0 )
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...