Update list values

EasyDictionary Programs · 62 of 94

Write a Python program to add 10 to every number in the first list and subtract 25 from the second.

Advertisement
Input
{"virat": [50, 30], "rohit": [40, 10]}
Output
{'virat': [60, 40], 'rohit': [15, -15]}

Approach

  1. Rebuild each list with a comprehension.

Solution

Python
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]}
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs