browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need Python help?

Get answers from our community of Python experts on BYTES! It's free.

lists to save in a tuple

Nader
Guest
 
Posts: n/a
#1: Jun 27 '08
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



Diez B. Roggisch
Guest
 
Posts: n/a
#2: Jun 27 '08

re: lists to save in a tuple


Nader wrote:
Quote:
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
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
 
Posts: n/a
#3: Jun 27 '08

re: lists to save in a tuple


Nader wrote:
Quote:
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

Tommy Grav
Guest
 
Posts: n/a
#4: Jun 27 '08

re: lists to save in a tuple



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.
Quote:
Quote:
Quote:
>>a = [1,2,3]
>>b = ['a','b','c']
>>zip(a,b)
[(1, 'a'), (2, 'b'), (3, 'c')]
Quote:
Quote:
Quote:
>>>
Cheers
Tommy

On Jun 9, 2008, at 9:27 AM, Nader wrote:
Quote:
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
Nader
Guest
 
Posts: n/a
#5: Jun 27 '08

re: lists to save in a tuple


On Jun 9, 3:34 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
Quote:
Nader wrote:
Quote:
Hello,
>
Quote:
I have two lists and would save them in a tuple.
>
Quote:
a = [1,2,3]
b = ['a','b','c']
>
Quote:
with the next statement I can do that:
>
Quote:
t = [(x,y), for x in a for y in b]
>
Quote:
This gives the next list:
>
Quote:
[(1,'a'),(1,'b'),(1,'c'), (2,'a'),(2,'b'),(2,'c'), (3,'a'),(3,'b'),
(3,'c')]
>
Quote:
But I want the next list:
>
Quote:
[(1,'a'),(2,'b'),(3,'c')]
>
Quote:
Would somebody tell me how I can solve this problem?
>
zip(a, b)
>
Diez
Thank you!
Closed Thread


Similar Python bytes