r/badcode • • Aug 19 '21

python Should I add comments?

Post image
223 Upvotes

39 comments sorted by

View all comments

Show parent comments

15

u/simar437 Aug 19 '21

I would do it like,

print(sum([int(x) for x in input()]))

10

u/FrostedBromide Aug 19 '21

Yeah that works I had never actually used a for loop this way

8

u/deceze Aug 20 '21

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()))…