Sets are collection of immutable elements with NO duplicate values.
A Set CANNOT contain mutable elements like list, dictionary, and sets.
A Set CAN contain immutable elements like, numbers, strings, and tuples.
Sets are commonly used to remove duplicate values from a collection of values in a list or tuple.
You can create a set by using the curly braces { } and putting elements separated by commas.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#### Creating Sets #### # Defining sets set1={2,2,3,1,1,4,4} set2={5,5,3,4,5,5} set3={'a','c','c','b','b'} print(type(set1)) # printing the sets print(set1) print(set2) print(set3) |
Sample Output

Sets does not support indexing
Sets cannot be iterated because they does not support indexing.
1 2 3 4 5 6 7 8 |
#### Sets does not support indexing # Defining a set set1={2,3,4,5} # sets does not support indexing # Below command will throw error! set1[2] |
Sample Output

Using set to Remove duplicate values
Python sets are frequently used to remove duplicate values from a list or tuple.
1 2 3 4 5 6 7 8 9 |
# Removing duplicate values # Defining a sample list and tuple sampleList=[2,3,4,4,4,5,5,5,8,2,'b','b'] sampleTuple=(2,3,'a','a',4,4,10) # Removing duplicates using set print(set(sampleList)) print(set(sampleTuple)) |
Sample Output

Common set commands
Sets are used primarily to get comparison between two set of values. Sets are built for the operations like intersection, union etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#### Common Set commands #### # Defining sets set1={2,2,3,1,1,4,4} set2={5,5,3,4,5} print('set1:',set1) print('set2:',set2) # set OR operation print('OR Operation:', set1 | set2) # set AND operation print('AND Operation:', set1 & set2) # What elements are present in set1 which are not in set2 print('uncommon elements:', set1 - set2) # What elements are present in set2 which are not in set1 print('uncommon elements:',set2 - set1) # adding a SINGLE new item to set set1.add(10) print('After adding new element:',set1) # adding a MULTIPLE new items to set set1.update([20,30,40]) print('After adding multiple new elements:',set1) # removing an element from the set set1.remove(30) print('After removing SINGLE element:',set1) # removing MULTIPLE elements from the set set1.difference_update({20,30}) print('After removing MULTIPLE elements:',set1) # clear() function will remove all elements print(set1.clear()) print(set1) |
Sample Output

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!