lists to save in a tuple

N

Nader

Hello,

I have two lists and would save them in a tuple.

a = [1,2,3]
b = ['a','b','c']

with the next statement I can do that:

t = [(x,y), for x in a for y in b]

This gives the next list:

[(1,'a'),(1,'b'),(1,'c'), (2,'a'),(2,'b'),(2,'c'), (3,'a'),(3,'b'),
(3,'c')]

But I want the next list:

[(1,'a'),(2,'b'),(3,'c')]

Would somebody tell me how I can solve this problem?

Regards,
Nader
 
D

Diez B. Roggisch

Nader said:
Hello,

I have two lists and would save them in a tuple.

a = [1,2,3]
b = ['a','b','c']

with the next statement I can do that:

t = [(x,y), for x in a for y in b]

This gives the next list:

[(1,'a'),(1,'b'),(1,'c'), (2,'a'),(2,'b'),(2,'c'), (3,'a'),(3,'b'),
(3,'c')]

But I want the next list:

[(1,'a'),(2,'b'),(3,'c')]

Would somebody tell me how I can solve this problem?

zip(a, b)

Diez
 
G

Gerhard Häring

Nader said:
Hello,

I have two lists and would save them in a tuple.

a = [1,2,3]
b = ['a','b','c']

with the next statement I can do that:

t = [(x,y), for x in a for y in b]

This gives the next list:

[(1,'a'),(1,'b'),(1,'c'), (2,'a'),(2,'b'),(2,'c'), (3,'a'),(3,'b'),
(3,'c')]

But I want the next list:

[(1,'a'),(2,'b'),(3,'c')]

Would somebody tell me how I can solve this problem?

Use the zip() builtin.

zip(a, b)

-- Gerhard
 
T

Tommy Grav

ActivePython 2.5.1.1 (ActiveState Software Inc.) based on
Python 2.5.1 (r251:54863, May 1 2007, 17:40:00)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1,2,3]
>>> b = ['a','b','c']
>>> zip(a,b) [(1, 'a'), (2, 'b'), (3, 'c')]
>>>

Cheers
Tommy

Hello,

I have two lists and would save them in a tuple.

a = [1,2,3]
b = ['a','b','c']

with the next statement I can do that:

t = [(x,y), for x in a for y in b]

This gives the next list:

[(1,'a'),(1,'b'),(1,'c'), (2,'a'),(2,'b'),(2,'c'), (3,'a'),(3,'b'),
(3,'c')]

But I want the next list:

[(1,'a'),(2,'b'),(3,'c')]

Would somebody tell me how I can solve this problem?

Regards,
Nader
 
N

Nader

Nader said:
I have two lists and would save them in a tuple.
a = [1,2,3]
b = ['a','b','c']
with the next statement I can do that:
t = [(x,y), for x in a for y in b]
This gives the next list:
[(1,'a'),(1,'b'),(1,'c'), (2,'a'),(2,'b'),(2,'c'), (3,'a'),(3,'b'),
(3,'c')]
But I want the next list:
[(1,'a'),(2,'b'),(3,'c')]

Would somebody tell me how I can solve this problem?

zip(a, b)

Diez

Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top