1

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] )
wjandrea
  • 28,235
  • 9
  • 60
  • 81
LED Fantom
  • 1,229
  • 1
  • 12
  • 32
  • "although its shorthand doing for loop is very convenient"—comprehensions are _not_ a shorthand for loops. Loops and comprehensions are different things. – ChrisGPT was on strike Dec 09 '20 at 02:57

1 Answers1

2

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?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    It might be worth adding that `str.join()` is special, and normally you wouldn't want to add the brackets, like with `sum`, `min`/`max`, `any`/`all`, etc. – wjandrea Dec 09 '20 at 02:24