beginner, idiomatic python

Scott David Daniels daniels at
Sat Aug 25 12:20:09 CEST 2007

bambam wrote:
>> The reason that lists don't have set-like methods is because
>> lists aren't sets -- lists can contain duplicate elements
and they are ordered.  I'd have used sets if I was sure you
meant [1,2,3] to mean the same thing as [3,1,2] and no duplicates.

> Interesting point -- if that's all there is in it, then lists should
> have difference and intersection methods. Not because they
> are the same as sets -- because they are slightly different than
> sets. 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.
Here you seem to be talking about multisets (also called bags).
They have more fully defined algebraic properties analogous to sets.

bag([1,2,3,3,4]) == bag([3,1,2,4,3]) != bag([1,2,3,4])
bag([1,2,2,3]) - bag([1,2]) == bag([2,3])
bag([1,2,3]) - bag([3,4]) == bag([1])

>>> Excellent. By symmetry, I see that "list" casts the set back into a list.
Some will say 'sorted' is a better conversion of a set to list, since
the result is well-defined.

--Scott David Daniels
Scott.Daniels at Acm.Org

More information about the Python-list mailing list