Updating Value List in Dictionary - Python
Last Updated :
28 Jan, 2025
We are given a dictionary where the values are lists and our task is to update these lists, this can happen when adding new elements to the lists or modifying existing values in them. For example, if we have a dictionary with a list as a value like this: {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3} then the output will be {'gfg': [2, 10, 12], 'is': 2, 'best': 3}.
Using List Comprehension
In this method we extract the key and iterate over its value (which is a list) using a list comprehension and that allows us to modify each element in the list directly such as doubling the values.
Python
li = {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3}
# Using list comprehension and Updating value list in dictionary
li['gfg'] = [x * 2 for x in li['gfg']]
print(str(li))
Output{'gfg': [2, 10, 12], 'is': 2, 'best': 3}
Explanation:
- list comprehension [x * 2 for x in li['gfg']] iterates over each element in the list and multiplies it by 2.
- dictionary value for the key 'gfg' is updated with the newly modified list.
Let's explore other methods to achieve the same:
Using map() + lambda
In this method, we combine map() with a lambda function to update the values in the dictionary where the values are lists. map() function applies the given function to each element in the list and modifying it as needed.
Python
li = {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3}
# Using map() + lambda to update value list in dictionary
li['gfg'] = list(map(lambda x: x * 2, li['gfg']))
print(str(li))
Output{'gfg': [2, 10, 12], 'is': 2, 'best': 3}
Explanation: map(lambda x: x * 2, li['gfg']) applies the lambda function to each element in the list by multiplying each element by 2 then the list() function is used to convert the result of map() back into a list.
Using update()
In this method we are using the update() function to modify the values of a dictionary. We create a temporary dictionary with the updated list and then update the original dictionary with this new value using update().
Python
li = {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3}
# Using update() to update value list in dictionary
temp = {'gfg': [x * 2 for x in li['gfg']]}
li.update(temp)
print(str(li))
Output{'gfg': [2, 10, 12], 'is': 2, 'best': 3}
Using for loop
This method involves iterating through the dictionary using a for loop and for each key we check if the associated value is a list and if so, we update the list by multiplying each element by 2.
Python
li = {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3}
# Using for loop to update value list in dictionary
for key in li:
if isinstance(li[key], list):
li[key] = [x * 2 for x in li[key]]
print(str(li))
Output{'gfg': [2, 10, 12], 'is': 2, 'best': 3}
Using the dict() constructor with a generator expression
In this method, we use the dict() constructor along with a generator expression. The generator iterates over each key-value pair in the original dictionary checking if the value is a list and if it is then it updates the list by multiplying each element by 2 otherwise the value remains unchanged.
Python
li = {'gfg' : [1, 5, 6], 'is' : 2, 'best' : 3}
# Use dict() constructor with generator expression to update the dictionary
d = dict((k, [x * 2 for x in v]) if isinstance(v, list) else (k, v) for k, v in li.items())
print(str(d))
Output{'gfg': [2, 10, 12], 'is': 2, 'best': 3}
Similar Reads
Inverse Dictionary Values List - Python We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]}Using defaul
2 min read
Get Python Dictionary Values as List - Python We are given a dictionary where the values are lists and our task is to retrieve all the values as a single flattened list. For example, given the dictionary: d = {"a": [1, 2], "b": [3, 4], "c": [5]} the expected output is: [1, 2, 3, 4, 5]Using itertools.chain()itertools.chain() function efficiently
2 min read
Python - Value Dictionary from Record List Sometimes, while working with Python Records lists, we can have problems in which, we need to reform the dictionary taking just values of the binary dictionary. This can have applications in many domains which work with data. Let us discuss certain ways in which this task can be performed. Method #1
6 min read
Python Update Dictionary Value by Key A Dictionary in Python is an unordered collection of key-value pairs. Each key must be unique, and you can use various data types for both keys and values. Dictionaries are enclosed in curly braces {}, and the key-value pairs are separated by colons. Python dictionaries are mutable, meaning you can
3 min read
Python | Iterate through value lists dictionary While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind of problem can occur in web development domain. Let's discuss certain ways in which this problem can be solved. Method #1: Using list comprehension List c
4 min read