Python Practice › Dictionary Programs › Program 62
Update list values
Write a Python program to add 10 to every number in the first list and subtract 25 from the second.
Input
{"virat": [50, 30], "rohit": [40, 10]}Output
{'virat': [60, 40], 'rohit': [15, -15]}Approach
- Rebuild each list with a comprehension.
Solution
d = {"virat": [50, 30], "rohit": [40, 10]}d["virat"] = [x + 10 for x in d["virat"]]d["rohit"] = [x - 25 for x in d["rohit"]]print(d)Output
{'virat': [60, 40], 'rohit': [15, -15]}Learn the concept
New to this topic? Read the lesson, then come back.