469,306 Members | 1,565 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

lists to save in a tuple

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
Jun 27 '08 #1
4 1168
Nader wrote:
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
Jun 27 '08 #2
Nader wrote:
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

Jun 27 '08 #3

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

On Jun 9, 2008, at 9:27 AM, Nader wrote:
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
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #4
On Jun 9, 3:34 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Nader wrote:
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
Thank you!
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Ivan Voras | last post: by
23 posts views Thread by Fuzzyman | last post: by
42 posts views Thread by Jeff Wagner | last post: by
4 posts views Thread by Peter Notebaert | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.