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
Reply


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

Forum Jump:

User Panel Messages

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