10 Things Every Python Developer Should know About Python Sets.
Photo by Anna Shvets from Pexels

10 Things Every Python Developer Should know About Python Sets.

Python has four collection data types — lists, tuples, dictionaries, and sets. Each one of these data types has different properties and methods. In this article, we are going to look at sets. I will break down ten(10) things that every Python user should know about Python sets.

1. You can create sets using the curly brackets {}or the set function

There are two ways you can create sets in Python. You can use the curly brackets{} or the set() function. In a set, elements are separated by commas. Let’s now create our first set using the curly brackets. We are going to create a set of four(4) elements. We will then use the type() function to confirm if we have created a set.

# Creating a set called animals
  animals = {'Lion','Tiger','Elephant', 'Cheetah'}
  print(type(animals))
	

  Output:
  <class 'set'>        

You can see from the output that the type() function has confirmed that what has been created is a set. However, you cannot create an empty set with curly brackets. This is because sets have similar brackets to dictionaries. When you run a code to check the class type of an empty collection with curly brackets you are going to get a dictionary, not a set. In the code below, we try to create an empty set of animals, but the type() function confirms that we have not created a set but a dictionary.

# creating an empty set
  animals ={}
  print(type(animals))
	

  Output:
  <class 'dict'>        

Another way we can create sets is by using the built-in sets function — set(). The function takes only one argument, so if we want to create a set of more than one element, we need to use double brackets. The code below will demonstrate that. We are going to recreate a set of animals using the set() function. The output of the code confirms that we have a set class type.

# creating a set using the set function
  animals = set(('Lion','Tiger','Elephant', 'Cheetah'))
  print(type(animals))
	

  Output:
  <class 'set'>        

2. Sets are not ordered

Elements in a set have no particular order; that is why you cannot use an index to access them. When we create a set below, take note of how the order of the elements changes when we print it out. We are going to create a set of random numbers and print them out. Notice how the output is in a different order to our original set? Every time you print out the set, the order may change because set elements have no fixed index.

# creating a set of numbers
  num_set ={12, 78, 19, 0, 78, 67, 55}
  print(num_set)
	

  Output:
  {0, 67, 19, 55, 12, 78}        

3. Sets don’t allow duplicates

If you try to have duplicates in a set, it will only recognize one of those elements and ignore the others. This makes sets a great alternative to lists and tuples if you are trying to get rid of duplicates in your data. Below is an example of what happens once we try to create a set with a duplicate. In our set of animals, we have put two(2) lions, but when we run print, we can only see one lion in the output. This is because a set can only recognize one lion. The other lion(duplicate) is ignored.

animals = {'Lion','Tiger','Elephant', 'Cheetah','Lion'
print(animals)
	

Output:
{'Cheetah', 'Elephant', 'Tiger', 'Lion'}}        

If an element is already in the set and we try to add it again, a set will simply ignore it.

4. Sets can have arbitrary elements

Sets can have floats, integers, strings, Boolean values, and so forth. We can have a set of arbitrary objects at the same time. The example below demonstrates just that. The output proves that our set with arbitrary elements is still valid.

mixed_set ={"Honda", "Suzuki", "Benz", 2, False, (2, 3, 8)}
print(mixed_set)
	

Output:
{False, 2, (2, 3, 8), 'Honda', 'Benz', 'Suzuki'}        

5. Sets are mutable, but the elements are immutable

Sets are mutable, you can add and remove elements from the set after it is created. However, the elements of the set are immutable. You cannot replace an element in a set. The two methods that you can perform on a set are add and remove. There is no replace method. We are going to demonstrate add and remove methods with example code.

Add Method

cars ={"Honda", "Suzuki", "Benz"}
# adding car to set
cars.add('Tesla')
print(cars)
	

Output:
{'Suzuki', 'Honda', 'Tesla', 'Benz'}        

Remove Method

cars ={'Suzuki', 'Honda', 'Tesla', 'Benz'
# removing car from set
cars.remove('Benz')
print(cars)
	

Output:
{'Honda', 'Suzuki', 'Tesla'}}        

You can also use the discard method to remove elements from a set. The remove method will always raise an exception if you try to remove an element that is not in a set, while the discard method will not.

6. You cannot have a nested set

You can have a nested list, tuple, and dictionary, but not a nested set. Since sets are mutable, but the elements in them are immutable, you cannot have a set inside a set because a set does not meet the condition of immutability. Below demonstrates what happens when we try to put a set inside another set. We get an error because a set cannot accept another set inside its brackets. Only immutable elements can be added to sets.

cars ={"Honda", "Suzuki", "Benz"
# adding set to set
cars.add({'Tesla','BMW'})
print(cars)
	

Output:
cars.add({'Tesla','BMW'})
TypeError: unhashable type: 'set'}        

Let’s see what happens when we try to add a tuple to a set.

cars = {"Honda", "Suzuki", "Benz"}
# adding tuple to set
cars.add(('Tesla','BMW'))
print(cars)
	

Output:
{'Honda', ('Tesla', 'BMW'), 'Suzuki', 'Benz'}        

We have successfully added a tuple to the set because tuples are immutable.

7. You cannot have a list inside a set

For the same reason you cannot have a set inside a set; you cannot have a list inside a set. Lists are mutable. Sets can only hold immutable elements. In the code below we get an unhashable error because a list has failed the immutability test.

cars ={"Honda", "Suzuki", "Benz"}
# adding set to set
cars.add(['Tesla','BMW'])
print(cars)
	

Output:
cars.add(['Tesla','BMW'])
TypeError: unhashable type: 'list'        

8. The pop method will pop a random element from a set

If you try to use a pop method to remove an element from a set, you will have no idea which item will be removed. This is because set elements have no position or index. Every time you run the pop method, a different item will be removed. In our example below, the pop method just removed ‘Suzuki’. If you try running the code again, a different item will likely be removed.

cars ={"Honda", "Suzuki", "Benz", "Tesla"}
cars.pop()
print(cars)
	

Output:
{'Benz', 'Tesla', 'Honda'}        

9. Frozen sets are immutable.

A frozen set is basically a set that does not support set methods that modify a set. You cannot add or remove items to the frozen set once it is created. Any operation that attempts to change the contents of a set cannot be performed on a frozen set. You can create a frozen set using the built-in function — fronzenset(). We are creating a frozen set using the built-in function below:

# creating a frozen set
  animals = frozenset(('Lion', 'Tiger', 'Elephant'))
  print(animals)
	

  Output:
  frozenset({'Lion', 'Elephant', 'Tiger'})        

10. Membership tests are faster in sets than lists.

When you iterate through a set to find a member (a in b), your operation will be faster than iterating through a list to find a member. The reason sets are a bit faster than lists when it comes to membership tests is because sets have a hash-table structure and are not indexed. When you search for an element in a set, the loop does not have to iterate through all the elements in a set to get to the desired member. However, searching for a member in a list requires that the loop iterates through all the elements; this makes lists slower than sets. You will notice the difference in speed between sets and lists when you have a large dataset. Below I have created a simple function that calculates how long it takes to do a membership test in sets and lists (searching for 79998890 in a range of 10000000). You can see from the output that sets are faster than lists. Please note that when you run this code you will get different results because the speed of the execution depends on your machine.


from time import time
	

a = range(10000000)
print(type(a))
set1 = set(a)
list1 = list(a)
i = 79998890
	

	

def speed_check(a):
	start = time()
	if i in a:
	    print('')
	end = time()
	speed = end-start
	return speed
	

	

print(f'Execution time for set is :{speed_check(set1)} seconds')
print(f'Execution time for list is :{speed_check(list1)} seconds')
	

        

Conclusion

There you have it; 10 things you should know about sets in Python. So, next time you want to get rid of duplicates in your dataset, you can always call on sets. If you want to create an immutable set, create a frozen set. If you find this article insightful, please do not hesitate to like, share and follow. Please check out my book below if you are starting your Python journey.


Deepak Kumar

Self-learner - Coding | Python | ML | AI |

3y

This helped me to clear the Set concept ☺️ thankyou so much Benjamin Phiri ACMA CGMA / Data Analyst

To view or add a comment, sign in

More articles by Benjamin Bennett Alexander

Insights from the community

Others also viewed

Explore topics