0
List comprehension with dictionary values
I want to make that for loop into a list comprehension https://code.sololearn.com/c3a21A4a168A/?ref=app
4 Réponses
+ 2
print(sum(num_exercises.values()))
+ 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())
+ 3
import functools
print(functools.reduce(lambda a,b:a+b,num_exercises.values()))
+ 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