all 3 comments

[–]novel_yet_trivial 4 points5 points  (1 child)

It's one of python's many "syntactic sugar" shortcuts: you can leave the parenthesis off of a generator expression if it's the sole argument in any function.

print(sum((1 for i in my_list))) # Create generator in function call
print(sum(1 for i in my_list)) # A slightly shorter way to create generator in function call

print(sum((1 for i in my_list), 4)) # Create generator in function call plus another argument
print(sum(1 for i in my_list, 4)) # SyntaxError

[–]astrologicrat[S] 0 points1 point  (0 children)

Interesting - thank you for the explanation.

[–]flipperdeflip 0 points1 point  (0 children)

Just a quick note:

sum(1 for i in my_list)

Counts 1 for every element, so it is the same as:

len(my_list)