Python Forum
Parallel iteration with for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parallel iteration with for loop

I'm really struggling in understanding how parallel iteration works with for loops.

Here is just some experimental code:

myList = list(zip(['a', 'b', 'c'],['abc', 'def', 'ghi'],[1, 2, 3]))

for i in myList:

for i,j,k in myList:

for i,j,k in myList:

for i,j in myList:
[('a', 'abc', 1), ('b', 'def', 2), ('c', 'ghi', 3)] ('a', 'abc', 1) ('b', 'def', 2) ('c', 'ghi', 3) a abc 1 b def 2 c ghi 3 a abc b def c ghi ValueError: too many values to unpack (expected 2)
I understand the first print statement and the first for loop.

The for loops with the i,j,k and i,j is where my understanding breaks down...

It's clearly iterating 3 times because it's printing 3 times..

My tired neurons can't figure out what's happening this late. Someone explain please Heart
(Jul-19-2019, 11:28 PM)Josh_Python890 Wrote: The for loops with the i,j,k and i,j is where my understanding breaks down...

It's clearly iterating 3 times because it's printing 3 times..
Its iterating 3 times because there are 3 tuples in the myList

This is failing
for i,j in myList:
because i and j are not enough for it to unpack. You need 3 for each, or one for the whole tuple, but not 2. You can print 2 as you have to your second to last for loop, but you must loop it with 3. Each element of the iterable is a tuple, then you can specify three variables and each element in the loop will be unpacked to the three.

Another way to view it would be
for first, second, third in [('a', 'abc', 1), ('b', 'def', 2), ('c', 'ghi', 3)]:
Where first is the chars, second is the strings, and third is the ints. If you omit second and third, then first would be the entire tuple. IF you only omit third, then you get a ValueError because its trying to unpack 3 into 2. Whereas if you have 4 in this case, you would get instead
ValueError: not enough values to unpack (expected 4, got 3)
Google tuple unpacking

Possibly Related Threads…
Thread Author Replies Views Last Post
  saving each iteration of a loop sgcgrif3 3 1,194 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  Pandas, How to trigger parallel loop Mekala 4 1,087 Oct-29-2020, 12:58 PM
Last Post: Mekala
  String slicing and loop iteration divyansh 9 2,357 Jun-07-2020, 10:29 PM
Last Post: divyansh
  Changing a variable's name on each iteration of a loop rix 6 33,480 Jan-03-2020, 07:06 AM
Last Post: perfringo
  parallel for loop with multiprocessing dervast 0 1,142 Jul-04-2019, 03:16 PM
Last Post: dervast
  Multiprocessing my Loop/Iteration (Try...Except) Jompie96 7 2,453 Jun-19-2019, 12:59 PM
Last Post: noisefloor
  First for loop stops after first iteration Divanova94 10 4,939 May-01-2019, 04:27 PM
Last Post: buran
  issue with updating list every iteration of a loop ftrillaudp 2 1,741 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  exception during iteration loop Skaperen 6 2,006 Oct-24-2018, 12:35 AM
Last Post: Skaperen
  For Loop, execute one time for every loop iteration dragan979 2 2,902 Feb-20-2018, 12:02 PM
Last Post: dragan979

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020