beginner, idiomatic python

bambam david at asdf.asdf
Mon Aug 27 07:47:25 CEST 2007


Thank you. I figured the set would probably be faster,
but the lists are small, and I'm concerned that the code
is going to look Byzantine if I keep swapping between
lists, sets and dictionaries :~).

At the moment there are no sets or dictionaries in the
entire code base I am working with. I'm not sure if the
place I am looking at right now is supposed to support
duplicates or not: duplicates are permitted, but they
cause report anomalies.

Steve.


"Paul Rubin" <http://phr.cx@NOSPAM.invalid> wrote in message 
news:7xk5rhvcq0.fsf at ruckus.brouhaha.com...
> "bambam" <david at asdf.asdf> writes:
>> 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.
>
> You get a temporary copy either way; note you're going to linearly
> search U on every pass.  Maybe you want:
>
>   SU = set(u)
>   A = [a for x in A if x in SU]
>
> or possibly
>
>   A = list(set(A) & set(U))
>
> which will remove duplicate elements from A and not necessarily keep
> them in the same order, but is likely to be fastest of the bunch. 





More information about the Python-list mailing list