Tutorial :How do I iterate over the tuples of the items of two or more lists in Python? [duplicate]



Question:

This question already has an answer here:

Specifically, I have two lists of strings that I'd like to combine into a string where each line is the next two strings from the lists, separated by spaces:

a = ['foo1', 'foo2', 'foo3']  b = ['bar1', 'bar2', 'bar3']  

I want a function combine_to_lines() that would return:

"""foo1 bar1  foo2 bar2  foo3 bar3"""  

I admit I've already solved this problem, so I'm going to post the answer. But perhaps someone else has a better one or sees a flaw in mine.

Update: I over-simplified my example above. In my real-world problem the lines were formatted in a more complicated manner that required the tuples returned from zip() to be unpacked. But kudos to mhawke for coming up to the simplest solution to this example.


Solution:1

It's not necessary to unpack and repack the tuples returned by zip:

'\n'.join(' '.join(x) for x in zip(a, b))  


Solution:2

The zip function "returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables."

def combine_to_lines(list1, list2):      return '\n'.join([' '.join((a, b)) for a, b in zip(list1, list2)])  


Solution:3

>>> a = ['foo1', 'foo2', 'foo3']  >>> b = ['bar1', 'bar2', 'bar3']  >>> for i in zip(a,b):  ...   print ' '.join(i)  ...  foo1 bar1  foo2 bar2  foo3 bar3  


Solution:4

Are you asking about the zip function?


Solution:5

I realize this is a very old question, but it's interesting to note that this can be seen as a matrix transposition.

>>> import numpy  >>> data = numpy.array([['foo1','foo2','foo3'],['bar1','bar2','bar3']])  >>> print(data)  [['foo1' 'foo2' 'foo3']   ['bar1' 'bar2' 'bar3']]  >>> print(data.transpose())  [['foo1' 'bar1']   ['foo2' 'bar2']   ['foo3' 'bar3']]  

If you're dealing with a large dataset or more lists this might be a more efficient solution.


Solution:6

Here's a one-liner. Could do x + ' ' + y if you were so inclined, not sure if it would be slower or not.

>>> a = ['foo1', 'foo2' , 'foo3']  >>> b = ['bar1', 'bar2', 'bar3']  >>> '\n'.join(' '.join([x,y]) for (x,y) in zip(a,b))  'foo1 bar1\nfoo2 bar2\nfoo3 bar3'  >>> print _  foo1 bar1  foo2 bar2  foo3 bar3  


Solution:7

'\n'.join(((str(x) + ' ' + str(y)) for (x, y) in zip(a, b)))  


Solution:8

Simple as:

" ".join([a[x] + " " + b[x] for x in range(len(a))])  


Solution:9

In python 3.x:

'\n'.join(' '.join(x) for x in zip(a, b))  

zip returns a list of tuples. zip stops when the shorter of a or b stops.

In python 2.x:

if itertools.izip would stop when one of the lists ran out of elements but itertools.izip_longest will stop till lists don't have any more elements

import itertools  '\n'.join(' '.join(x) for x in itertools.izip(a, b))    '\n'.join(' '.join(x) for x in itertools.izip_longest(a, b))  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »