hi there sirdari gagilap here for infoworld at idg in this episode of smart python we're going
to dive a little deeper into sets and learn how they can be used in more advanced ways
we started exploring sets in a previous video where i showed how their collections of unique
objects i also talked a little bit about how you can add and remove items from sets but sets have
other much more powerful manipulations that you can perform with them they're a great boon when
you want to filter or merge or compare groups of distinct objects very quickly
in this example i have two sets s1 and s2 each containing the titles of some movies they have
some elements in common and some not and with them i'm going to demonstrate the various operations
that you can perform between sets now if you've studied set theory in math these operations
ought to be really familiar they're basically implementations of common operations in set theory
first up we have unions between sets this takes everything from both sets and merges them into a
single set removing any duplicates this we do by way of the pipe operator note that the original
sets in this operation and any other operations that we're performing unless i say otherwise
are not changed the result is a new third set we can also derive the intersection of two sets
this is a set that contains everything that's only in both sets in this case the only common element
is blade runner and we use the ampersand operator to perform a set intersection
the difference operation takes what's in one set and returns everything in it that is not in the
second set in this case we have two movies alien and 2001 a space odyssey that are in
set one but not in set two but we don't get blade runner because we already have that in both sets
to use this operation we use the minus sign now note that the order of the sets for this
operation makes a difference if we did set 2 minus set one we would get very different results
the symmetric difference operation gives us what's in set one and set two but not what's in
both of them at the same time since both sets have blade runner we don't have that in the resulting
operation but we get everything else from both sets for this one we use the carat operator
but in this case the order of the operation doesn't matter since we're just merging the two
another operation that may sound exotic but is really pretty simple is the disjoint operation
all this does is test if both sets have nothing in common and returns a boolean value s1 and s2
do have an element in common blade runner so for that we would get false but i've created another
set s3 alien and solaris and i've performed a disjoint operation against s1 with it and
that yields true because those two sets don't have anything in common by the way there is no operator
that can be used to perform a disjoint we have to use a method from the set type as we see here
subset is also pretty simple it checks if all elements of one set are contained within another
set the set blade runner s4 is a subset of s1 because we have blade runner in s1 so that will
return true and a set is considered a subset of itself since all of its elements are contained
within the so-called other set to test for this we would use the less than equal sign
another version of subset is the proper subset this is the same as a regular
subset except that the two sets cannot be the same thing so s4 is a proper subset of s1 but
s1 is not a proper subset of itself to test for a proper subset we use the less than sign
we also have superset and proper superset which use the greater than equals and equal
signs and those as you can imagine test if one set contains all of the elements of another set
and the proper version of it requires that the two sets not be the same thing
now i'm going to give a more concrete demonstration of how sets can be used this
script here takes two directories two different installations of python from the same machine
and creates sets from the files found in each of these directories
it then uses the difference operator to produce a set that contains everything that's in the python
3.9 directory but not in the older python 3.8 directory this is much faster and more
convenient than trying to gravel through each directory separately and compare them manually
the print process takes far more time to run than the actual set operation does now we can also do
the reverse we can find all the files in python 3.8 that aren't in 3.9 and we can use in this
example the intersection operator to find out what files are common to both installations of python
so to sum up sets are best when you want to test for the existence of things in collections
or perform operations based on the presence or absence of those things in one or more collections
they're not substitutes for lists because they don't guarantee the order of elements in them
and they're not indexable but they're better than lists for finding out whether or not something
is present in them since you can test for the membership or presence of something in a set far
faster than you can with a list that's it for this episode if you liked it please leave a
comment below don't forget to subscribe to the idg tech talk channel and for more smart
python and more dev with serdar be sure to follow me on facebook youtube and infowl.com
you