Python's set datatype

Here's a little tip for the friday. Python's set datatype was added in Python2.4 and it's really great and can be used in different situations.

First, when you want to filter out a list to extract only unique items:

print set([1, 1, 3, 4, 5, 3, 2])

Second, when you want to find out how two lists relate, for example, find out all the elements they have in common:

print set([1, 1, 3, 4, 5, 3, 2]).difference([1, 2])
print set([1, 1, 3, 4, 5, 3, 2]).intersection([1, 2])

That's it :)

Code · Python · Tips 6. Feb 2009
6 comments so far

Can I add my tip as well? I wrote an article on set operations in Unix shell a while ago. Do all the ops with diff, sort, uniq, comm, grep, and other Unix tools:

Set Ops in Unix Shell

That's it. :)

Peteris aka. the shell ninja ;-)

You seem to be missing a [ in the last print:

(also i need to put this ] here, otherwise the parser for the amiformat seems to dislike my [ code ] entry)

print set([1, 1, 3, 4, 5, 3, 2]).difference(1, 2])

Should be:

print set([1, 1, 3, 4, 5, 3, 2]).difference([1, 2])

Mads

Thanks Mads, it's now corrected. I'll also note your AmiFormat bug. I think thought it's because `code` is meant to be a block and you write it as an one liner.

As a side note, it's also good to know about symmetric_difference:

In [1]: set([1,2]).difference([1,2,3])
Out[1]: set([])
In [1]: set([1,2]).symmetric_difference([1,2,3])
Out[1]: set([3])

Looks good.

Post a comment
Commenting on this post has expired.
© 2000-2009 amix. Powered by Skeletonz.