how to separate a list into two lists?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Aug 6 21:35:23 EDT 2011


Chris Angelico wrote:

> On Sun, Aug 7, 2011 at 1:58 AM, Tim Roberts <timr at probo.com> wrote:
>> I did momentarily consider the following slimy solution:
>> L1 = dict(L).keys()
>> L2 = dict(L).values()
>> but that reorders the tuples.  They still correspond, but in a different
>> order.
>>
> 
> Which can be overcome with collections.OrderedDict. But what's dict(L)
> going to do? It's going to loop over L, more than once in fact.
> 
> I guess the real question is: Why do you wish to avoid a loop?

I think what the Original Poster actually meant was he wanted to avoid
*writing out an explicit loop*. That is, he wants a one-liner, so he
doesn't have to think about the details of iterating over the list.

When we write:

a = sum(a_sequence)

aren't we doing the same thing really?


-- 
Steven




More information about the Python-list mailing list