Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10977 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2011-08-06 18:14 +0100 |
| Last post | 2011-08-07 10:27 +1000 |
| Articles | 2 — 2 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.
Re: how to separate a list into two lists? Chris Angelico <rosuav@gmail.com> - 2011-08-06 18:14 +0100
Re: how to separate a list into two lists? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-07 10:27 +1000
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-08-06 18:14 +0100 |
| Subject | Re: how to separate a list into two lists? |
| Message-ID | <mailman.1984.1312650859.1164.python-list@python.org> |
On Sat, Aug 6, 2011 at 6:07 PM, smith jack <thinke365@gmail.com> wrote: > 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] > > i do not want to use loop, any methods to make this done? One easy way is to use list comprehensions. Technically that'll involve a loop, but the loop is handled efficiently under the hood. L1 = [x[0] for x in L] L2 = [x[1] for x in L] Another way would be to construct a dictionary from your list of tuples and then use the keys() and values() of that dictionary to form your lists (use collections.OrderedDict if the order matters). But I think the list comps are the best way. ChrisA
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-08-07 10:27 +1000 |
| Message-ID | <4e3ddbed$0$29994$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #10977 |
Chris Angelico wrote:
> On Sat, Aug 6, 2011 at 6:07 PM, smith jack <thinke365@gmail.com> wrote:
>> 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]
>>
>> i do not want to use loop, any methods to make this done?
>
> One easy way is to use list comprehensions. Technically that'll
> involve a loop, but the loop is handled efficiently under the hood.
>
> L1 = [x[0] for x in L]
> L2 = [x[1] for x in L]
I hardly think that's "under the hood". It's right there: for x in L. How
much more explicitly a loop do you want?
To the original poster, Jack: if you don't loop over the list, how do you
expect to operate on each and every item?
Whether you write:
L1, L2 = [], []
for a,b in L:
L1.append(a)
L2.append(b)
or
L1, L2 = zip(*L)
or
L1 = [x[0] for x in L]
L2 = [x[1] for x in L]
you are still looping over the list. In the second case, the one-liner using
zip, you loop *twice*: once to unpack L into separate arguments, the second
time to zip them up. And the result you end up with are tuples, not lists,
so if you need lists, you have to loop *again*, over each tuple, converting
them to lists. Here's one way to do that while keeping it a one-liner:
L1, L2 = map(list, zip(*L))
If L is small, it really doesn't matter what you do, they will all be more
or less as efficient. Write whatever you feel looks best.
But if L is huge, then the difference between a four-liner that iterates
over the list once, and one-liner that iterates over it four times, may be
considerable.
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web