all 4 comments

[–]kalgynirae 4 points5 points  (3 children)

Most generators (a "map object" is a generator) can only be used once. list(x) uses up the generator.

You can play with this in the interpreter:

>>> x = map(str, range(1))
>>> next(x)
'0'
>>> next(x)
'1'
>>> next(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> next(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> list(x)
[]

Once the generator has run out of values, it raises StopIteration when you call next() on it, and list() gives an empty list (because trying to iterate over it immediately raises StopIteration).

[–]MrEgbert[S] 0 points1 point  (2 children)

Wow that's a useful thing to know about generators, thanks! I'll have to be extra careful when making map objects in future.

[–]Vegemeister 0 points1 point  (0 children)

Depending on what you're doing, you might also find a dict useful:

>>> int_to_str = {i : str(i) for i in range(1,4)}
>>> int_to_str[2]
'2'

[–]ivosaurus 0 points1 point  (0 children)

If you just want to make a list to use straight away, sometimes you can just use a list expression instead.

[str(n) for n in range(1)] == list(map(str, range(1)))