Skip to content
Advertisement

What type of iterable is “x for x in y”?

I’ve been confused of list comprehension in Python, although its shorthand doing for loop is very convenient. I’m not sure if what’s in the join() function’s parameter below is list comprehension since usually you put [] around to show it’s a list comprehension.

My question: What’s the type of iterable produced by str(x) for x in res[::-1] in join() below? Thanks.

ex,

''.join( str(x) for x in res[::-1] )

Advertisement

Answer

This is a generator expression, which itself is an iterable.

The square brackets could still be added, however. Refer Joining strings. Generator or list comprehension?

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement