Python Forum
iterating over N lists in parallel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
iterating over N lists in parallel
#1
i had been thinking this might be a nice feature but i am wondering if this could be done with a generator.  the original idea was:

for a,b,c in [1,2],['three','four'],['foo','bar']:
    print(a,b,c)
Output:
1 three foo 2 four bar
it would be parallel iteration over 3 separate lists of the same length.  if the lengths differ iteration stops at the end of the shortest one.

could this be done with any iterator?

Moderator Kebap: Moved thread to General Coding
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Do you know python zip()?
Reply
#3
there i go again, not knowing the right words to search, although i didn't do it recently. i guess it is best for each thing i need to do in python is assume it has already been done. i guess i got this from C experience, where almost nothing had been done, and from assembler 360 experience where there was even less.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
The length of each list should be equal to or more than the no of values we provide (a,b,c...)

for a,b,c in [1,2,3],[4,5,6],[7,8,9]:
      print (a,b,c)

the output is:
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)


this is no need to use same data type ,any data type can be used as list member.
Reply
#5
(Mar-23-2017, 08:53 AM)ankit Wrote: The length of each list should be equal to or more than the no of values we provide (a,b,c...)

for a,b,c in [1,2,3],[4,5,6],[7,8,9]:
      print (a,b,c)

the output is:
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)


this is no need to use same data type ,any data type can be used as list member.

what i am wanting is for the 3 lists to be iterated in parallel so that the first iteration gets [1,4,7].
Output:
lt1/forums /home/forums 11> py3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> for x in zip([1,2,3],[4,5,6],[7,8,9]): ...     print(x) ... (1, 4, 7) (2, 5, 8) (3, 6, 9) >>> lt1/forums /home/forums 12>
this is exactly what i was looking for.  the name probably comes from the action of a zipper for the case of 2 iterators.  this will let me iterate over N lists in parallel without using indexes or constructing a list of N-tuples.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
i wrote a little script using zip() ... not what i was originally thinking about this for.  it is in Scripts & Snippets.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
Just for sake of completeness and to the benefit of Python2 users - in Python2 zip returns list, while in itertools.izip() returns iterator. In Python3 zip returns iterator and itertools.izip() is not available.
Also Python2 has itertools.izip_longest () and in Python3 there is itertools.zip_longest() to process iterables of unequal size and option to provide a fill value for shorter one(s).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Iterating through lists with different letter cases fatherted99 2 1,833 Jun-07-2020, 01:22 PM
Last Post: deanhystad
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,190 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Iterating over two lists in python dgm 2 2,975 Jul-24-2018, 08:31 PM
Last Post: dgm
  Best form of iterating over a lsit of lists tonymcgregor 15 7,564 Oct-15-2017, 11:19 PM
Last Post: tonymcgregor

Forum Jump:

User Panel Messages

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