A set in Python is a unique data structure based on the mathematical concept of a set. It does not follow any order or index. Elements in a set are written inside curly braces.

# This program is an example of set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
print(testSet)

Result:

{'C', 'Java', 'C#', 'Python', 'C++'}

Accessing a Specific Element

Since sets are unordered, elements cannot be accessed by index. To check if an element is in a set, use the in operator, which returns True or False.

# This program is an example of set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
print("Python" in testSet)

Result:

True

Looping Through a Set

You can loop through each element in a set.

# This program is an example of looping set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
for i in testSet:
    print(i)

Result:

C++                                                                                                                                                                                
C                                                                                                                                                                                  
Java                                                                                                                                                                               
C#                                                                                                                                                                                 
Python

Set Methods

Similar to other complex data types, sets have specific methods for various operations. Here are some of them (in alphabetical order).

add()

Adds a new element to the set.

# This program is an example of add() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
testSet.add("Swift") # add() method

print(testSet)

Result:

{'C#', 'C++', 'C', 'Python', 'Swift', 'Java'}

clear()

Removes all elements from the set.

# This program is an example of clear() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
testSet.clear() # clear() method

print(testSet)

Result:

set()

copy()

Copies all elements of a set into another set.

# This program is an example of copy() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
outputSet=testSet.copy() # copy() method

print(outputSet)

Result:

{'C++', 'C', 'C#', 'Python', 'Java'} 

del()

Deletes the entire set.

# This program is an example of del() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
del(testSet) # del() method

print(setLength)

Result:

Traceback (most recent call last):                                                                                                                                                 
  File "main.py", line 7, in <module>                                                                                                                                              
    print(setLength)                                                                                                                                                               
NameError: name 'setLength' is not defined

An error occurred because we tried to print a set that has been deleted.

difference()

Returns the difference between two sets.

# This program is an example of difference() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
testSet2={"PHP","Python","Javascript"} # Defining a set
 
outputSet=testSet.difference(testSet2) # difference() method
outputSet2=testSet2.difference(testSet) # difference() method

print(outputSet)
print(outputSet2)

Result:

{'C', 'C#', 'Java', 'C++'}                                                                                                                                                         
{'Javascript', 'PHP'}

In the 6th line, the first set is subtracted by the second set, leaving the unique values of the first set. The 7th line repeats the same process with the second set.

difference_update()

Similar to difference(), but it also removes the common elements from the original set.

# This program is an example of difference_update() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
testSet2={"PHP","Python","Javascript"} # Defining a set
 
outputSet=testSet.difference_update(testSet2) # difference_update() method

print(testSet)

Result:

{'C#', 'C', 'C++', 'Java'}

Here, the original set is printed without the common element “Python”.

discard()

Removes a specific element from the set.

# This program is an example of discard() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
testSet.discard("Java") # discard() method

print(testSet)

Result:

{'Python', 'C', 'C++', 'C#'} 

intersection()

Returns the common elements of two or more sets.

# This program is an example of intersection() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
testSet2={"PHP","Python","Javascript"} # Defining a set
 
outputSet=testSet.intersection(testSet2) # intersection() method

print(outputSet)

Result:

{'Python'}

intersection_update()

Similar to difference_update(), but it keeps only the common elements in the original set.

# This program is an example of intersection_update() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
testSet2={"PHP","Python","Javascript"} # Defining a set
 
outputSet=testSet.intersection_update(testSet2) # intersection_update() method

print(testSet)

Result:

{'Python'} 

isdisjoint()

Returns True if two sets have no common elements, otherwise False.

# This program is an example of isdisjoint() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
testSet2={"PHP","Python","Javascript"} # Defining a set
 
outputSet=testSet.isdisjoint(testSet2) # isdisjoint() method

print(outputSet)

Result:

False

issubset()

Checks if one set is a subset of another.

# This program is an example of issubset() method for set in python
  
testSet={"Python"} # Defining a set
testSet2={"PHP","Python","Javascript"} # Defining a set
 
outputSet=testSet.issubset(testSet2) # issubset() method

print(outputSet)

Result:

True

Since “Python” is in testSet2, testSet is a subset.

issuperset()

Checks if one set is a superset of another.

# This program is an example of issuperset() method for set in python
  
testSet={"Python"} # Defining a set
testSet2={"PHP","Python","Javascript"} # Defining a set
 
outputSet=testSet.issuperset(testSet2) # issuperset() method
outputSet2=testSet2.issuperset(testSet) # issuperset() method

print(outputSet)
print(outputSet2)

Result:

False                                                                                                                                                                              
True

len()

Returns the number of elements in a set.

# This program is an example of len() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
setLength=len(testSet)

print(setLength)

Result:

5

pop()

Removes and returns a random element from the set.

# This program is an example of pop() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
poppedValue=testSet.pop() # pop() method

print(poppedValue)
print(testSet)

Result:

C#                                                                                                                                                                                 
{'Python', 'C++', 'Java', 'C'} 

Here, “C#” was popped from the set.

remove()

The remove() method is used to delete a specific element from a set. It functions similarly to discard(), but if the element is not present in the set, discard() will not raise an error, whereas remove() will.

# This program is an example of remove() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
 
testSet.remove("Java") # remove() method

print(testSet)

testSet.remove("Swift") # remove() method

print(testSet)

Result:

{'Python', 'C#', 'C++', 'C'}                                                                                                                                                       
Traceback (most recent call last):                                                                                                                                                 
  File "main.py", line 9, in <module>                                                                                                                                              
    testSet.remove("Swift") # remove() method                                                                                                                                      
KeyError: 'Swift'

In the 5th line, the remove operation is successfully completed. However, in the 9th line, attempting to remove “Swift” results in an error since it is not in the set.

union()

The union() method is used to perform the union operation on sets.

# This program is an example of union() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
testSet2={"PHP","Javascript"} # Defining a set
 
outputSet=testSet.union(testSet2)

print(outputSet)

Result:

{'C#', 'Python', 'PHP', 'Java', 'Javascript', 'C', 'C++'}

update()

The update() method is used to update a set with elements from another set.

# This program is an example of update() method for set in python
  
testSet={"C","C++","C#","Java","Python"} # Defining a set
testSet2={"PHP","Javascript"} # Defining a set
 
testSet.update(testSet2)

print(testSet)

Result;

{'C++', 'C', 'Python', 'PHP', 'C#', 'Java', 'Javascript'}
0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x