Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10978 > unrolled thread

Re: how to separate a list into two lists?

Started byEmile van Sebille <emile@fenx.com>
First post2011-08-06 10:24 -0700
Last post2011-08-07 10:13 +1000
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: how to separate a list into two lists? Emile van Sebille <emile@fenx.com> - 2011-08-06 10:24 -0700
    Re: how to separate a list into two lists? bud <only@fleshwound.org> - 2011-08-06 18:21 +0000
      Re: how to separate a list into two lists? Chris Angelico <rosuav@gmail.com> - 2011-08-06 19:35 +0100
      Re: how to separate a list into two lists? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-07 10:13 +1000

#10978 — Re: how to separate a list into two lists?

FromEmile van Sebille <emile@fenx.com>
Date2011-08-06 10:24 -0700
SubjectRe: how to separate a list into two lists?
Message-ID<mailman.1986.1312651348.1164.python-list@python.org>
On 8/6/2011 10:07 AM smith jack said...
> if a list L is composed with tuple consists of two elements, that is
> L = [(a1, b1), (a2, b2) ... (an, bn)]
>
> is there any simple way to divide this list into two separate lists , such that
> L1 = [a1, a2... an]
> L2=[b1,b2 ... bn]
>

 >>> L = [('a1', 'b1'), ('a2', 'b2'),('an', 'bn')]
 >>> zip(*L)
[('a1', 'a2', 'an'), ('b1', 'b2', 'bn')]


Emile

[toc] | [next] | [standalone]


#10982

Frombud <only@fleshwound.org>
Date2011-08-06 18:21 +0000
Message-ID<4e3d8634$0$5872$c3e8da3$12bcf670@news.astraweb.com>
In reply to#10978
On Sat, 06 Aug 2011 10:24:10 -0700, Emile van Sebille wrote:

> On 8/6/2011 10:07 AM smith jack said...
>> if a list L is composed with tuple consists of two elements, that is L
>> = [(a1, b1), (a2, b2) ... (an, bn)]
>>
>> is there any simple way to divide this list into two separate lists ,
>> such that L1 = [a1, a2... an]
>> L2=[b1,b2 ... bn]
>>
>>
>  >>> L = [('a1', 'b1'), ('a2', 'b2'),('an', 'bn')] zip(*L)
> [('a1', 'a2', 'an'), ('b1', 'b2', 'bn')]
> 

Nice. :)  I forgot about zip, still learning Python myself. 

I'll have to check up on the *L - is that a reference?
I know in Perl, you can assign the lhs to a list,
below works because there are exactly 2 items on the rhs.
Does Python have a catchall, or an ignore the rest?
Example, if L was a tuple that had 3 or more items, 
the below lhs would fail.  Is this possible in Python?


(X,Y) = zip(*L)


X
: ('a1', 'a2', 'an')

Y
: ('b1', 'b2', 'bn')


[toc] | [prev] | [next] | [standalone]


#10983

FromChris Angelico <rosuav@gmail.com>
Date2011-08-06 19:35 +0100
Message-ID<mailman.1990.1312655755.1164.python-list@python.org>
In reply to#10982
On Sat, Aug 6, 2011 at 7:21 PM, bud <only@fleshwound.org> wrote:
> Nice. :)  I forgot about zip, still learning Python myself.
>
> I'll have to check up on the *L - is that a reference?
> I

It expands the list into the arguments. It's the parallel to:

def func(*args):

which collapses the args into a list.

ChrisA

[toc] | [prev] | [next] | [standalone]


#10993

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-08-07 10:13 +1000
Message-ID<4e3dd88f$0$29994$c3e8da3$5496439d@news.astraweb.com>
In reply to#10982
bud wrote:

> I'll have to check up on the *L - is that a reference?

No, as Chris already answered, unary * is used for packing and unpacking
positional arguments to functions; unary ** is similarly used for
collecting keyword arguments.


> I know in Perl, you can assign the lhs to a list,
> below works because there are exactly 2 items on the rhs.
> Does Python have a catchall, or an ignore the rest?

Yes. In Python 3, you can do this:

>>> a, b, *t, c = (1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> t
[3, 4, 5, 6, 7, 8]

(but not in Python 2)


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web