standings
[{'driver': 'Max Verstappen', 'team': 'Red Bull', 'home_country': 'Netherlands', 'points': 0}, {'driver': 'Lewis Hamilton', 'team': 'Mercedes', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'George Russell', 'team': 'Mercedes', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'Sergio Perez', 'team': 'Red Bull', 'home_country': 'Mexico', 'points': 0}, {'driver': 'Lando Norris', 'team': 'McLaren', 'home_country': 'United Kingdom', 'points': 0}, {'driver': 'Charles Leclerc', 'team': 'Ferrari', 'home_country': 'Monaco', 'points': 0}, {'driver': 'Valtteri Bottas', 'team': 'Ferrari', 'home_country': 'Spain', 'points': 0}]
driver_with_points
{'Carlos Sainz': 18, 'Charles Leclerc': 26, 'Esteban Ocon': 6, 'Fernando Alonso': 2, 'George Russell': 12, 'Kevin Magnussen': 10, 'Lewis Hamilton': 15, 'Valtteri Bottas': 8, 'Yuki Tsunoda': 4, 'Zhou Guanyu': 1}
I have to update the standings list depending on the points of driver in driver_with_points
dictionary, if driver name exists in driver_with_points
dictionary.
The task is done simply by the following lines.
for driver in standings:
if driver["driver"] in drivers_with_points.keys():
driver["points"] = drivers_with_points[driver["driver"]]
But I need to do the similar task by using list comprehension.
Can anyone help me convert this for loop to list comprehension?
I have tried
[driver["points"]=drivers_with_points[driver["driver"]] for driver in standings if driver["driver"] in drivers_with_points.keys()]
but it's error Unresolved reference 'drivers_with_points