beginner, idiomatic python

Mikael Olofsson mikael at isy.liu.se
Fri Aug 24 05:39:13 EDT 2007



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