You can remove the [] to make it a generator expression, which makes this more efficient. Instead of having to turn the entire thing into a list, which then gets iterated by sum again, just have each element be processed one by one. Or, you know, sum(map(int, input()))…
Very clever. Looks like it works as intended, and this may well be considered the "pythonic" way to solve this problem - although as noted below, list comprehensions are more readable and clear.
Anyhow, I'm writing this to say that "clever" is often a problem in software. I don't think that this is bad code - it works, and someone who understands (or is willing to look up) the calls can see how it works. But neat and clever tricks are fragile, and fragility is a direct path to agony in professional software development.
View all comments
Show parent comment
15
u/FrostedBromide Aug 19 '21 edited Aug 19 '21
It's actually supposed to print the sum of the digits of the input and would throw an error if a float value is entered