Python Forum

Full Version: itertools and chain
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Can someone please help me understand what is the difference between this t = list(itertools.chain(*([1,2,3],[4,5,6]))) and y = list(itertools.chain(([1,2,3],[4,5,6]))).

The first one gives this result [1,2,3,4,5,6] whilst the second one gives a list of lists i.e. [[1,2,3],[4,5,6]]
That's the difference!
* take function argument and expands it into actual positional arguments in the function call.
def bar(seq, a=None, b=None):
    print(seq, a, b)

>>> bar([1, 2, 3])
[1, 2, 3] None None

>>> bar(*[1, 2, 3])
1 2 3