0

Heyo

I'm learning Python and I came across this example:

''.join(str(e) for e in blank)

which returns:

'sadfsdfsd_ _ _ _ _ _ _ _ '

I read this and that example makes sense

So the code str(e) for e in blank is the sequence that is passed to the join method. But why doesn't it run independently?

str(e) for e in blank
  File "<stdin>", line 1
    str(e) for e in blank
             ^
SyntaxError: invalid syntax

blank btw is = ['text', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ', '_ ']

Jwan622
  • 11,015
  • 21
  • 88
  • 181
  • 1
    `(str(e) for e in blank)` is a generator expression. It creates a generator object, which is an iterable, but not a sequence. TutorialsPoint is not... the best resource. Note, if you check the current docs of Python, the argument is named `iterable`, so it takes *any* iterable, not just sequences (which are things like `list`, `tuple`, `str`, `bytes`, and `range` objects). Sequences are objects that have have a `__len__` and are indexable from `0-(len(sequence)-1)` – juanpa.arrivillaga Jan 14 '18 at 23:19
  • Why doesn't the sequence generator work by itself? – Jwan622 Jan 15 '18 at 21:19
  • There is no "sequence generator". You have a *generator expression* without parentheses, which are required *with the exception* of when you pass it as a single argument. You can use `(str(e) for e in blank)` as a valid generator expression. – juanpa.arrivillaga Jan 15 '18 at 21:41

0 Answers0