s = [1,2,3,4,5,6,7,8,9]
n = 3
list(zip(*[iter(s)]*n)) # returns [(1,2,3),(4,5,6),(7,8,9)]
How does zip(*[iter(s)]*n) work? What would it look like if it was written with more verbose code?
This is a technique used for splitting a list into chunks of equal size – see that question for a general overview of the problem.
iter()is an iterator over a sequence.[x] * nproduces a list containingnquantity ofx, i.e. a list of lengthn, where each element isx.*argunpacks a sequence into arguments for a function call. Therefore you’re passing the same iterator 3 times tozip(), and it pulls an item from the iterator each time.