Sets in python provide a method to create a unique set of unordered items with no duplicates. Their main use case is for checking if an item exists in a set of items, which can be useful in many different situations. Creating a set is pretty easy, and is kind of similar to . The only difference, is we use curly brackets to define a set: how we define lists in Python {} mySet = { "some", "set", "of", "items" } Sets can also be defined from lists using the function: set() mySet = set([ 'some', 'list', 'becoming', 'a', 'set' ]) # set is { 'some', 'list', 'becoming', 'a', 'set' } You can also create sets from strings, using the same function: set() mySet = set('somestring') # set is { 's', 'o', 'm', 'e', 's', 't', 'r', 'i', 'n', 'g' } As with other countable types of data, we can use to get the length of a set, too: len let mySet = set([ 'some', 'list', 'becoming', 'a', 'set' ]) print(len(mySet)) # Returns 5 Finally, we can also define what is known as a , which is simply an immutable, unchangeable version of a set with a fixed value, using the function: frozenset frozenset() let mySet = frozenset([ 'some', 'list', 'becoming', 'a', 'set' ]) Combining and Intersecting Sets We can combine two sets into one using the operator. If an item exists in both sets, only one copy of it will be brought over. Here's an example where we combine two sets: | mySet = { "set", "one" } myNewSet = { "set", "two" } combinedSet = mySet | myNewSet print(combinedSet) # { "set", "one", "two" } We can sets using . That means we'll end up with a set where the items are only items that exist in both. Using the same example, we can therefore create a set only containing the item : intersect & set mySet = { "set", "one" } myNewSet = { "set", "two" } combinedSet = mySet & myNewSet print(combinedSet) # { "set" } Another way we can combine sets is by subtraction, o end up with a new set that only contains items left when removing any common items in both sets. For example, the new set below only has one item - , since and both contain "set" and "one": cool mySet mySecondSet mySet = { "set", "one", "cool" } mySecondSet = { "set", "one" } myNewSet = mySet - mySecondSet print(myNewSet) # { "cool" } Finally, we can do what is called , where we end up with a set that contains items found in either or , but not both: symmetric difference mySet mySecondSet mySet = { "set", "one", "cool", "nice" } mySecondSet = { "set", "one", "friendly" } myNewSet = mySet ^ mySecondSet print(myNewSet) # { "cool", "nice", "friendly" } Testing Membership using Sets The main use case for sets is , to see if an item exists in a set. We can do this using the and keywords. Let's look at an example. If we want to check is in our set, we use : testing membership in not in orange fruits in fruits = { "orange", "apple", "peach" } print("orange" in fruits) # True Or, if we want to check if orange is not in , we use : fruits not in fruits = { "orange", "apple", "peach" } print("orange" not in fruits) # False Making a copy of a set As with lists, we can make a copy of a set using the method attached to all sets. This will not change the value but will change the reference in memory for this new set. That means that if compared by value using , the sets will be the same, when compared by reference using , the sets will not be the same: copy() == is mySet = { "set", "one" } mySetCopy = mySet.copy(); print(mySet == mySetCopy) # True print(mySet is mySetCopy) # False Testing for Supersets and Subsets Another really useful use case for sets is the ability to check if a set is a superset or subset of another set (which is a bit of a tongue twister): subsets will be sets that are fully contained within another set. supersets will be sets that contain fully the members of another set. Checking for Subsets in Python Let's say we have two sets, as shown below: mySet = { "set", "one", "two" } mySecondSet = { "set", "one" } , is in fact a subset of , since it is fully contained within . We can test for this using the operator: mySecondSet mySet mySet <= mySet = { "set", "one", "two" } mySecondSet = { "set", "one" } print(mySecondSet <= mySet) # True We can also use the operator to check for , meaning that is contained within , but is not equal in value to . In the example above, this is also true: < true subsets mySecondSet mySet mySet mySet = { "set", "one", "two" } mySecondSet = { "set", "one" } print(mySecondSet < mySet) # True In the following example, however, is indeed a subset of , but it is not a , since both are equal in value: mySecondSet mySet true subset mySet = { "set", "one", } mySecondSet = { "set", "one" } print(mySecondSet <= mySet) # True print(mySecondSet < mySet) # False Checking for Supersets in Python Super sets work exactly the same way as subsets - the only difference is the arrow is the opposite way around. So is used to check for supersets, while is used to check for any supersets. Using our example from before, is a superset of - so the following returns true: > true >= mySet mySecondSet mySet = { "set", "one", "two" } mySecondSet = { "set", "one" } print(mySet > mySecondSet) # True And similarly, while mySet is a superset of below, it is not a true superset, so does not return true, while does: mySeconSet > >= mySet = { "set", "one", } mySecondSet = { "set", "one" } print(mySet >= mySecondSet) # True print(mySet > mySecondSet) # False Testing if two sets have completely different values in Python Sometimes, you'll also want to check if two sets are completely original when compared to each other. For example, , and are two sets with unique values when compared to each other. In Python, the function allows us to accomplish that: { "one", "two" } { "three", "four" } isdisjoint mySet = { "one", "two", } mySecondSet = { "three", "four" } print(mySet.isdisjoint(mySecondSet)) # True Other Set Methods While everything we've talked about so far applies both to s and s, there are also a few other methods available to s, which allow us to mutate their value. These are: frozenset set set - adds an item to the set. set.add('item') - removes an item from the set. set.remove('item') - adds all items from to the original . This can also be written as set.update(newSet) newSet set set |= newSet - removes all items from a set set.clear() - removes the 4th item from a set, or the last item if no number is specified set.pop(4) - keeps only items found in both and . Can also be written as set.intersection_update(newSet) set newSet set &= newSet - takes , and removes any items found in . Can also be written as set.difference_update(newSet) set newSet set -= newSet - keeps only found in either and , but not both. Can also be written as set.symmetric_difference_update(newSet) set newSet set ^= newSet While the first 5 provide easy ways to add and remove items from sets, the last 3 are the same as what we talked about before when we covered intersecting and combining sets. The difference here is we can use these functions to change the itself. While this is possible on normal sets, we cannot apply these methods to a . set frozenset Conclusion That should be everything you need to know about sets in Python. I hope you've enjoyed this guide. I've also written more about all of the . If you've enjoyed this guide, you might also enjoy . different data structures available in Python here my other engineering content here Thanks for reading! You can learn more about Python data collections below: Python Data Collections Python Data Collections: Lists Python Data Collections: Tuples Python Data Collections: Sets Python Data Collections: Dictionaries Also published . here