beginner, idiomatic python

bambam david at asdf.asdf
Mon Aug 27 05:41:17 CEST 2007


Is it safe to write

A = [x for x in A if x in U]

or is that undefined? I understand that the slice operation
can be used to make a temporary copy, so I could write

A=[x for x in A[:] if x in U]

but I've just copied that without any understanding.

Steve.


"bambam" <david at asdf.asdf> wrote in message 
news:13d49lk6ue2as6d at corp.supernews.com...
> That looks good, and perhaps a difference operator
> would be too simple to be useful anyway.
>
> Steve.
>
> "Mikael Olofsson" <mikael at isy.liu.se> wrote in message 
> news:mailman.238.1187948359.32294.python-list at python.org...
>>
>>
>> bambam wrote:
>>>
>>> In this case it doesn't matter - my lists don't contain
>>> duplicate elements this time - but I have worked with lists in
>>> money market and in inventory, and finding the intersection
>>> and difference for matching off and netting out are standard
>>> operations.
>>
>> I would use a list comprehension for that case:
>>
>> A = ['a','b','c','a','c','d']
>> U = ['a','b','e']
>> B = [x for x in A if x in U]
>>
>> The result would be B==['a','b','a']
>>
>> /MiO
>
> 





More information about the Python-list mailing list