How to pair two lists togther into a paired list?

This is the place for queries that don't fit in any of the other categories.

How to pair two lists togther into a paired list?

Postby romana » Sun Jun 16, 2013 5:27 am

Been teaching myself python but of course, I'm new to it. What I'm trying to do is pair two lists with one another.
Code: Select all
lst2=[1,2,3,4,5]
lst3=['a','b','c','d','e'] 

should result in:
Code: Select all
[[1,'a'],[2,'b'],[3,'c'],[4,'b'],[5,'e']]


My best shot is using a for loop which, clearly, I do not fully understand:

Code: Select all
lst1 = []
for i in lst2:
    lst1[i] = [lst2[i]],lst3[i]]

print lst1


Point me away from my erroneous ways, O brothers, great python sages!

tldr; how do I pair list1[1] with list2[1] to make list3[1] = [list1[1],list2[1]]?
Last edited by Yoriz on Sun Jun 16, 2013 2:02 pm, edited 1 time in total.
Reason: Editted the title a bit
romana
 
Posts: 2
Joined: Sun Jun 16, 2013 5:13 am

Re: List pairing/iteration [newbie question]

Postby micseydel » Sun Jun 16, 2013 8:10 am

The built-in zip() function does what you want, basically this
Code: Select all
zipped = []
for index in xrange(min(len(lst2), len(lst3))):
    zipped.append([lst2[index], lst3[index]])

In practice though, use zip*.

* In fact, to replicate zip you need indexes, but they're a pain and prone to error so in real Python code people use code like
Code: Select all
pairs = [("cats, "dogs"), ("hot", "cold"), ("smart", "not smart")]
for first, second in pairs:
    print first, "!=", second

So we'd like to write the first one like
Code: Select all
for first, second in zip(lst2, lst3):
    zipped.append([first, second])

but obviously you could just use the result of zip() instead of iterating over it!
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How to pair two lists togther into a paired list?

Postby romana » Mon Jun 17, 2013 6:20 pm

Ah I see. So we can specify multiple elements in a for loop. Thank you!
romana
 
Posts: 2
Joined: Sun Jun 16, 2013 5:13 am

Re: How to pair two lists togther into a paired list?

Postby ochichinyezaboombwa » Mon Jun 17, 2013 8:17 pm

romana wrote:Ah I see. So we can specify multiple elements in a for loop.

-- Only if your list (or any iterable) consists of such items (can be lists or tuples).

This is called "tuple unpacking", like in:
Code: Select all
x = (1,5)
a,b = x # a== 1, b == 5


It just happens that the for loop can unpack your items on the fly automatically as well.
Code: Select all
>>> l = zip (range(1,5), range(10,50,10), range(100,500,100) )
>>> l
[(1, 10, 100), (2, 20, 200), (3, 30, 300), (4, 40, 400)]
>>> for left, middle, right in l:
...     print left, middle, right
...
1 10 100
2 20 200
3 30 300
4 40 400
# equivalent to a longer version:
>>> for tpl in l:
...     left, middle, right = tpl # explicit unpacking
...     print left, middle, right
...
1 10 100
2 20 200
3 30 300
4 40 400

It's an important Python idiom which you will find everywhere.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests