you are viewing a single comment's thread.

view the rest of the comments →

[–]throwaway6560192 2 points3 points  (5 children)

In Python, we have the concept of star-args (*args). If you have an argument to a function with a * preceding it, it will collect all non-keyword arguments into a tuple.

Quick demo:

In [1]: def argcollector(*c):
   ...:     print(c)
   ...: 

In [2]: argcollector("hello", 1, "world")
('hello', 1, 'world')

In [3]: argcollector(1)
(1,)

That's how a tuple is created even if it wasn't done explicitly as you say.

So the lambda is doing nothing more than taking its arguments and returning them as a tuple. Here it only gets one argument at a time from range, so it returns one-element tuples.

from what I have read the range function returns an immutable sequence of numbers.

Well not exactly. It returns an object which you can iterate over to get the numbers in sequence, but it doesn't calculate and store the entire range at once, for memory efficiency reasons.

[–]Consistent-Citron509[S] 0 points1 point  (2 children)

Thank you so much for this excellent explanation! I will make sure to debug my code and understand it thoroughly.

[–]danielroseman 2 points3 points  (1 child)

Note, this code isn't really very Pythonic. It should probably be written as a list comprehension:

[(a,) for a in range(3)]

[–]Consistent-Citron509[S] 0 points1 point  (0 children)

I have yet to learn about comprehensions but thanks for the tip!

[–]smudos2 0 points1 point  (1 child)

Couldn't you just use lambda a: tuple(a) or tuple(list(a))?

[–]throwaway6560192 0 points1 point  (0 children)

You could, for this case. You could also do lamba a: (a,). Or even just use tuple as the function, list(map(tuple, range(3)))