Python sets

A set in Python is a built-in data type used to store unique elements—that is, it automatically removes duplicates. Sets are defined using curly braces {}, and they are unordered and unindexed, meaning the items have no specific position or order.

While sets are generally unchangeable in terms of individual items (you can’t change elements directly), you can still add or remove items from the set as a whole.

Sets are useful when you want to store a collection of items without duplicates and don’t care about the order in which they’re stored

				
					set1 = {1, 3, 4}
				
			

Duplicate values are ignored in sets

				
					set2 = {1, 3, 4, 1}
				
			
				
					print(set2)
				
			

Example with true and 1

We can add different data types to a set.

True in Python is equivalent to the integer 1, hence “True would not be printed” since a set cannot have a duplicate.

				
					set2a = {1, 3, 4, True}
				
			
				
					print(set2a)
				
			

Here we also have a mixture of things.

				
					set3 = {1, 'Nolan', 4, True}
				
			

let us check the length

				
					len(set3)
				
			

Here we add to the set

				
					set3.add("Baseball")
				
			
				
					print(set3)
				
			

Here we remove from the ste.

				
					set3.remove('Baseball')
				
			

Note that if we have already removed the item and we call the remove() method again, it throws an error.

				
					set3.remove('Baseball')
				
			
				
					set3.discard('Nolan')
				
			

To fix this, use discard().

This wont throw an error even though the value to discard is missing.

				
					set3.discard('Nolan')
				
			

We use the .clear() to clear the set.

				
					set3.clear()
				
			
				
					print(set3)
				
			
				
					set3 = {1, 'Nolan', 4, True}
				
			

Two sets can be merged using the union() method or | operator.

Duplicates gets removed when using union

				
					union_test = set3.union(set2)
				
			
				
					union_test
				
			
				
					union_test2 = set3 | set2
				
			
				
					union_test2
				
			
 
We use the intersection() or & operator.
Elements common to both sets are selected when using the intersection() or the & operator
				
					intersection_test = set3.intersection(set2)
				
			
				
					intersection_test
				
			
				
					intersection_test2 = set3 & set2
				
			
				
					intersection_test2
				
			

In Python, the difference() is used to refer to the elements that are in one set but not in the other

				
					differences_test = set3.difference(set2)
				
			
				
					differences_test
				
			

We can also use the – operator to achieve the same result as the difference() method

				
					differences_test2 = set3 - set2
				
			
				
					differences_test2
				
			

Multiple example

Here we are using the & operator to find elements common to multiple sets.

				
					mul1 = {1, 11, 7}
mul2 = {6, 11, 7}
mul2 = {13, 11, 9}
				
			
				
					multiple_test = mul1 & mul2 & mul2
				
			
				
					multiple_test
				
			

Let’s look at in and not in.

Here, we check if 11 is in multiple_test

Then we check if 23 is not in multiple_test

Remember, multiple_test is the variable we used to store our sets previously.

				
					11 in multiple_test
				
			
				
					23 not in multiple_test
				
			
				
					set1 = {11, 12, 5, 1}
set2 = {6, 8, 4}
set3 = {6, 8, 4, 11, 11}
				
			

Here we use the isdidjoint() method to check wheter two sets have no elements in common.

The isdisjoint() method checks whether two sets have no elements in common.

it returns True of False depending on the case.

				
					set1.isdisjoint(set2)
				
			

The issubset() method checks whether all elements of one set exist in another set

Here, we check if every element of set2 is found in set3.

it returns True or False depending on the case.

				
					set2.issubset(set3)
				
			

issuperset() is the reverse of .issubset().

Here we check if set3 contains all elements of set2.

				
					set3.issuperset(set2)
				
			

Here we use a for loop to loop through the set elements.

				
					hof_pitchers = {
    "Tom Seaver",
    "Randy Johnson",
    "Nolan Ryan",
    "Greg Maddux",
    "Pedro Martinez"
}

# Looping through the set elements
for pitcher in hof_pitchers:
    print(pitcher)
				
			

note. we cannot access an element in the set using indexing.

i.e hot_pitchers[0].

hence we need to convert the set to a list using our list() method

				
					# Convert set to a list
pitchers_list = list(hof_pitchers)
				
			

Now, we can access any element since we have converted it to a list.

				
					print("A Baseball Hall of Fame pitcher at position 3:", pitchers_list[3])
				
			

Here we create an empty set.

				
					emptyset = set()
				
			

The sorted() function can be used to create a new, sorted list from the elements of a set.

Python provides handy built-in functions that can be used directly on sets containing numeric values:

sum(set) – Returns the total of all the values in the set.

min(set) – Returns the smallest value in the set.

max(set) – Returns the largest value in the set.

cheatsheet.

add() Adds an element to the set
clear() Removes all elements from the set
copy() Returns a copy of the set
difference() Returns the difference of two or more sets as a new set
difference_update() Removes all elements of another set from this set
discard() Removes an element from the set if it is a member. (Do nothing if the element is not in set)
intersection() Returns the intersection of two sets as a new set
intersection_update() Updates the set with the intersection of itself and another
isdisjoint() Returns True if two sets have a null intersection
issubset() Returns True if another set contains this set
issuperset() Returns True if this set contains another set
remove() Removes an element from the set. If the element is not a member, raises a KeyError
symmetric_difference() Returns the symmetric difference of two sets as a new set
symmetric_difference_update() Updates a set with the symmetric difference of itself and another
union() Returns the union of sets in a new set
update() Updates the set with the union of itself and others

https://realpython.com/python-sets/

https://www.geeksforgeeks.org/python-sets/

https://learnpython.com/blog/python-sets/

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *