Python | Updating value list in dictionary
While working with dictionary with list as value is always vulnerable of getting it’s value updated. The ways or shorthands to perform this task can be handy in such situations. This can occur in web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension
The naive method to perform this particular task, in this, we just extract the key and then iterate over it’s value in list comprehensive format in packed list. This solved the problem.
# Python3 code to demonstrate working of # Updating value list in dictionary # Using list comprehension # Initialize dictionary test_dict = {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Using list comprehension # Updating value list in dictionary test_dict['gfg'] = [x * 2 for x in test_dict['gfg']] # printing result print("Dictionary after updation is : " + str(test_dict)) |
The original dictionary : {‘is’: 2, ‘gfg’: [1, 5, 6], ‘best’: 3}
Dictionary after updation is : {‘is’: 2, ‘gfg’: [2, 10, 12], ‘best’: 3}
Method #2 : Using map() + lambda
This task can be performed using the combination of above two functions in which we use the map() to link the function of updation to each of element of value list and lambda is used to specify the updation.
# Python3 code to demonstrate working of # Updating value list in dictionary # Using map() + lambda # Initialize dictionary test_dict = {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3} # printing original dictionary print("The original dictionary : " + str(test_dict)) # Using map() + lambda # Updating value list in dictionary test_dict['gfg'] = list(map(lambda x:x * 2, test_dict['gfg'])) # printing result print("Dictionary after updation is : " + str(test_dict)) |
The original dictionary : {‘is’: 2, ‘gfg’: [1, 5, 6], ‘best’: 3}
Dictionary after updation is : {‘is’: 2, ‘gfg’: [2, 10, 12], ‘best’: 3}
Recommended Posts:
- Python | Clearing list as dictionary value
- Python | Get dictionary keys as a list
- Python | Convert a list to dictionary
- Python | List value merge in dictionary
- Python | List of tuples to dictionary conversion
- Python | Converting list string to dictionary
- Python | Sort dictionary keys to list
- Python | Convert list of tuple into dictionary
- Python | Grouping list values into dictionary
- Python | Find dictionary matching value in list
- Python | Sort the list alphabetically in a dictionary
- Python | Initialize a dictionary with only keys from a list
- Python | Dictionary to list of tuple conversion
- Python | Get Unique values from list of dictionary
- Python | Combining values from dictionary of list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


