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

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]))
print(myList)

for i in myList:
    print(i)

for i,j,k in myList:
    print(i,j,k)

for i,j,k in myList:
    print(i,j)

for i,j in myList:
    print(i,j,k)
Output:
[('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
Reply
#2
(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
Error:
ValueError: not enough values to unpack (expected 4, got 3)
Google tuple unpacking
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Inconsistent loop iteration behavior JonWayn 2 954 Dec-10-2022, 06:49 AM
Last Post: JonWayn
  parallel loop python caro 4 1,735 Jun-16-2022, 08:46 PM
Last Post: woooee
  saving each iteration of a loop sgcgrif3 3 6,642 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  Pandas, How to trigger parallel loop Mekala 4 2,649 Oct-29-2020, 12:58 PM
Last Post: Mekala
  String slicing and loop iteration divyansh 9 4,614 Jun-07-2020, 10:29 PM
Last Post: divyansh
  Changing a variable's name on each iteration of a loop rix 6 84,033 Jan-03-2020, 07:06 AM
Last Post: perfringo
  parallel for loop with multiprocessing dervast 0 1,988 Jul-04-2019, 03:16 PM
Last Post: dervast
  Multiprocessing my Loop/Iteration (Try...Except) Jompie96 7 4,511 Jun-19-2019, 12:59 PM
Last Post: noisefloor
  First for loop stops after first iteration Divanova94 10 8,716 May-01-2019, 04:27 PM
Last Post: buran
  issue with updating list every iteration of a loop ftrillaudp 2 3,006 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp

Forum Jump:

User Panel Messages

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