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


Messages In This Thread
Parallel iteration with for loop - by Josh_Python890 - Jul-19-2019, 11:28 PM
RE: Parallel iteration with for loop - by metulburr - Jul-19-2019, 11:50 PM

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,737 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,651 Oct-29-2020, 12:58 PM
Last Post: Mekala
  String slicing and loop iteration divyansh 9 4,617 Jun-07-2020, 10:29 PM
Last Post: divyansh
  Changing a variable's name on each iteration of a loop rix 6 84,043 Jan-03-2020, 07:06 AM
Last Post: perfringo
  parallel for loop with multiprocessing dervast 0 1,989 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,719 May-01-2019, 04:27 PM
Last Post: buran
  issue with updating list every iteration of a loop ftrillaudp 2 3,007 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