0

List comprehension with dictionary values

I want to make that for loop into a list comprehension https://code.sololearn.com/c3a21A4a168A/?ref=app

11th Jun 2021, 8:37 PM
Jace🎭
Jace🎭 - avatar
4 Respostas
+ 2
print(sum(num_exercises.values()))
11th Jun 2021, 11:46 PM
visph
visph - avatar
+ 5
The for loop does not produce a list so list comprehension won't work. Why do you want to replace the loop? You could replace the loop with this: total_exercises = sum(num_exercises.values())
11th Jun 2021, 9:06 PM
Simon Sauter
Simon Sauter - avatar
+ 3
import functools print(functools.reduce(lambda a,b:a+b,num_exercises.values()))
11th Jun 2021, 9:04 PM
Abhay
Abhay - avatar
+ 1
Of course hahaha so that was the problem... I was just trying to find a short and more elegant way to write that code, I know that with list comprehension comes up really nice code but if there's other way then it works too
11th Jun 2021, 9:11 PM
Jace🎭
Jace🎭 - avatar