5

I'm trying to iterate over 3 elements at a time using the following (which seems to work):

for a, b, c in zip(*[iter(accounts_iter)]*3):
    print(a)
    print(b)
    print(c)
  1. accounts_iter is a list of tuples I created.
  2. iter(accounts_iter) unpacks accounts_iter into a list iterator, so its still a list of tuples just that its now iterable

Here's where I struggle to understand.

  • *[iter(accounts_iter)] unpacks the list in Step 2 into separate tuples, so no longer a list of tuples but individual tuples
  • *[iter(accounts_iter)]*3 multiplies that by 3

In my output, I do not seem to have duplicates, but am indeed able to process 3 items each loop.

But won't I have 3 copies of the same tuple once zip(*[iter(accounts_iter)]*3) happens?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
orangecat94
  • 145
  • 3

0 Answers0