Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pair up two lists? [duplicate]

Tags:

python

list

I'm a bit of a Python beginner so I apologise if this is a very basic question.

I have two lists of data which are obtained from:

with filein as f:
        reader=csv.reader(f)
        xs, ys = zip(*reader)

I would like to create a loop which would take the first item in "xs" and the first item in "ys" and print them out. I would then like to loop back and repeat for the second item in both lists and so forth.

I had thought something like:

for x in xs and y in ys:

Or

for x in xs:
    for y in ys:

But neither of these seems to give the desired result.

like image 683
user1813343 Avatar asked May 06 '13 15:05

user1813343


People also ask

How do I find duplicates in between two lists?

Select both columns of data that you want to compare. On the Home tab, in the Styles grouping, under the Conditional Formatting drop down choose Highlight Cells Rules, then Duplicate Values. On the Duplicate Values dialog box select the colors you want and click OK. Notice Unique is also a choice.

How do you unify two lists in Python?

One simple and popular way to merge(join) two lists in Python is using the in-built append() method of python. The append() method in python adds a single item to the existing list. It doesn't return a new list of items. Instead, it modifies the original list by adding the item to the end of the list.


1 Answers

For a one line you can use combination of map() and lambda(). Look here if not familiar to this concepts.

But be careful, you must be with python 3.x so that print is a function and can be used inside the lambda expression.

>>> from __future__ import print_function
>>> l1 = [2,3,4,5]
>>> l2 = [6,7,3,8]
>>> list(map(lambda X: print(X[0],X[1]), list(zip(l1,l2))))

output

2 6
3 7
4 3
5 8
like image 183
kiriloff Avatar answered Sep 27 '22 20:09

kiriloff