##### Parallel iteration with for loop
 Parallel iteration with for loop Josh_Python890 Programmer named Tim Posts: 8 Threads: 6 Joined: Sep 2017 Reputation: Jul-19-2019, 11:28 PM 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 Reply metulburr The Castle of aaarrrrggh Posts: 5,091 Threads: 392 Joined: Sep 2016 Reputation: Jul-19-2019, 11:50 PM (This post was last modified: Jul-19-2019, 11:50 PM by metulburr.) (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: BBcode, Forum Rules and Instructions, How to ask smart questions, the Basics, Classes, Python Gotchas Reply

 Possibly Related Threads… Thread Author Replies Views Last Post saving each iteration of a loop sgcgrif3 3 515 Jul-27-2021, 01:02 PM Last Post: DeaD_EyE Pandas, How to trigger parallel loop Mekala 4 929 Oct-29-2020, 12:58 PM Last Post: Mekala String slicing and loop iteration divyansh 9 2,007 Jun-07-2020, 10:29 PM Last Post: divyansh Need help on Parallel Programming Kishore01 0 446 Mar-23-2020, 03:14 PM Last Post: Kishore01 Changing a variable's name on each iteration of a loop rix 6 27,584 Jan-03-2020, 07:06 AM Last Post: perfringo parallel for loop with multiprocessing dervast 0 1,024 Jul-04-2019, 03:16 PM Last Post: dervast Multiprocessing my Loop/Iteration (Try...Except) Jompie96 7 2,128 Jun-19-2019, 12:59 PM Last Post: noisefloor First for loop stops after first iteration Divanova94 10 4,259 May-01-2019, 04:27 PM Last Post: buran issue with updating list every iteration of a loop ftrillaudp 2 1,528 Oct-29-2018, 03:23 AM Last Post: ftrillaudp exception during iteration loop Skaperen 6 1,769 Oct-24-2018, 12:35 AM Last Post: Skaperen

Forum Jump:

### User Panel Messages

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