Jan-23-2023, 04:28 AM
(This post was last modified: Jan-23-2023, 04:28 AM by deanhystad.)
Lots of ways to do this. Here are a 3 more.
from functools import reduce values = '1,2,3,4,5' print(sum(int(n) for n in values.split(','))) print(sum(map(int, values.split(',')))) print(reduce(lambda a, b: a + int(b), values.split(','), 0))I presented the choices in my order of preference. Comprehensions are very useful and something you need to know how to use. map() is like a one-trick-pony version of a comprehension. I don't find reduce() very useful.