Python Menu

Set Sequence in Python


In python, the set can be defined as the unordered collection of various items enclosed within the curly braces. The elements of the set cannot be duplicate. The elements of the python set must be immutable.

Unlike other collections in python, there is no index attached to the elements of the set, i.e., we cannot directly access any element of the set by the index. However, we can print them all together or we can get the list of elements by looping through the set.

Creating a set

The set can be created by enclosing the comma separated items with the curly braces.

Syntax:
Set={value1, value2….}

Example:
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}  
print(Days)  
print(type(Days))  
print("Looping through the set elements ... ")  
for i in Days:  
    print(i)
Output:
{'Wednesday', 'Tuesday', 'Sunday', 'Friday', 'Thursday', 'Saturday', 'Monday'}

Looping through the set elements...
Wednesday
Tuesday
Sunday
Friday
Thursday
Saturday
Monday

Set Operators

In Python, we can perform various mathematical operations on python sets like union, intersection, difference, etc

Operator Description
| Union Operator
& Intersection Operator
- Difference Operator:

☞ Union (|) Operator

The union of two sets are calculated by using the or (|) operator. The union of the two sets contains the all the items that are present in both the sets.

Example:
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1 | Days2)
Output:
{'Thr', 'Fri', 'Sun', 'Tue', 'Wed', 'Mon', 'Sat'}

☞ Intersection (&) Operator

The & (intersection) operator is used to calculate the intersection of the two sets in python. The intersection of the two sets are given as the set of the elements that common in both sets.

Example:
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1 & Days2)
Output:
{'Mon', 'Sat'}

☞ Difference (-) Operator

The difference of two sets can be calculated by using the subtraction (-) operator. The resulting set will be obtained by removing all the elements from set 1 that are present in set 2.

Example:
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1 - Days2)
Output:
{'Tue', 'Wed'}

Set Functions

Python contains the following methods to be used with the sets. Those are

  • len(set)
  • max(set)
  • min(set)
  • sum(set)
  • sorted(set)
  • set()
  • add()
  • update()
  • discard()
  • remove()
  • pop()
  • clear()
  • union()
  • intersection()
  • difference()
  • issubset()
  • issuperset()

☞ len()

In Python len() is used to find the length of set,i.e it returns the number of items in the set.

Syntax:
len(set)

Example:
num={1,2,3,4,5,6}
print(“length of set :”,len(num))
Output:
length of set : 6

☞ max()

In Python max() is used to find maximum value in the set.

Syntax:
max(set)

Example:
num={1,2,3,4,5,6}
lang={'java','c','python','cpp'}
print("Max of set :",max(num))
print("Max of set :",max(lang))
Output:
Max of set : 6
Max of set : python

☞ min()

In Python min() is used to find minimum value in the set.

Syntax:
min(set)

Example:
num={1,2,3,4,5,6}
lang={'java','c','python','cpp'}
print("Min of set :",min(num))
print("Min of set :",min(lang))
Output:
Min of set: 1
Min of set : c

☞ sum()

In python, sum(set) function returns sum of all values in the set. Set values must in number type.

Syntax:
sum(set)

Example:
num={1,2,3,4,5,6}
print(“sum of set items :”,sum(num))
Output:
sum of set items: 21

☞ sorted()

In python, sorted (set) function is used to sort all items of set in an ascending order. It also sorts the items into descending and ascending order. It takes an optional parameter 'reverse' which sorts the set into descending order.

Syntax:
sorted (set[,reverse=True])

Example:
num={1,3,2,4,6,5}
lang={'java','c','python','cpp'}
print(sorted(num))
print(sorted(lang))
print(sorted(num,reverse=True))
Output:
{1, 2, 3, 4, 5, 6}
{'c', 'cpp', 'java', 'python'}
{6, 5, 4, 3, 2, 1}

☞ set()

The set() method takes sequence types and converts them to sets. This is used to convert a given string or list or tuple into set.

Syntax:
set(sequence)

Example:
set1=set("PYTHON")
print(set1)
days=["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
set2 = set(days)  
print(set2) 
days=("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun")
set3 = set(days)
print(set3)
Output:
{'N', 'O', 'T', 'H', 'P', 'Y'}
{'Fri', 'Thur', 'Tue', 'Sun', 'Mon', 'Sat', 'Wed'}
{'Fri', 'Thur', 'Tue', 'Sun', 'Mon', 'Sat', 'Wed'}

☞ add()

In python, the add() method used to add some particular item to the set.

Syntax:
set.add (item)

Example:
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}  
print("\n printing the original set ... ")  
print(Days)
Days.add("Saturday");
Days.add("Sunday");
print("\n Printing the modified set..."); 
print(Days)
Output:
printing the original set ...
{'Wednesday', 'Friday', 'Thursday', 'Tuesday', 'Monday'}

 Printing the modified set...
{'Wednesday', 'Sunday', 'Friday', 'Thursday', 'Tuesday', 'Saturday', 'Monday'}

☞ update()

Python provides the update () method to add more than one item in the set.

Syntax:
set.update ([item1, item2…])

Example:
Months={"Jan","Feb","Mar","Apr"}
print("\n Printing the original set ... ")  
print(Months)
Months.update (["May","Jun","Jul"])
print("\n Printing the modified set..."); 
print(Months)
Output:
Printing the original set ...
{'Mar', 'Apr', 'Jan', 'Feb'}
 Printing the modified set...
{'Mar', 'Apr', 'Jan', 'Jun', 'May', 'Jul', 'Feb'}

☞ discard()

Python provides discard () method which can be used to remove the items from the set. If item doesn't exist in the set, the python will not give the error. The program maintains its control flow.

Syntax:
set.discard (item)

Example:
Months={"Jan","Feb","Mar","Apr"}
print("\n printing the original set ... ")  
print(Months)
Months.discard("Apr")
print("\n Printing the modified set..."); 
print(Months)
Months.discard("May")		#doesn’t give error
print("\n Printing the modified set..."); 
print(Months)
Output:
printing the original set ...
{'Jan', 'Apr', 'Mar', 'Feb'}
 Printing the modified set...
{'Jan', 'Mar', 'Feb'}
 Printing the modified set...
{'Jan', 'Mar', 'Feb'}

☞ remove()

Python provides remove () method which can be used to remove the items from the set. If item doesn't exist in the set, the python will give the error.

Syntax:
set.remove (item)

Example:
Months={"Jan","Feb","Mar","Apr"}
print("\n printing the original set ... ")  
print(Months)
Months.remove("Apr")
print("\n Printing the modified set..."); 
print(Months)
Months.remove("May")		#it give error
print("\n Printing the modified set..."); 
print(Months)
Output:
printing the original set ...
{'Feb', 'Jan', 'Apr', 'Mar'}
Printing the modified set...
{'Feb', 'Jan', 'Mar'}
KeyError: 'May' doesn’t exist.

☞ pop()

In Python, pop () method is used to remove the item. However, this method will always remove the last item.

Syntax:
set.pop ()

Example:
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}  
print("\n printing the original set ... ")  
print(Days)
Days.pop()
print("\n Printing the modified set..."); 
print(Days)
Output:
Printing the original set...
{'Monday', 'Wednesday', 'Friday', 'Tuesday', 'Thursday'}
Printing the modified set...
{'Wednesday', 'Friday', 'Tuesday', 'Thursday'}

☞ clear()

In Python, clear () method is used to remove the all items in set.

Syntax:
set.clear ()

Example:
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}  
print("\n printing the original set ... ")  
print(Days)
Days.clear()
print("\n Printing the modified set..."); 
print(Days)
Output:
printing the original set ...
{'Monday', 'Wednesday', 'Friday', 'Tuesday', 'Thursday'}
Printing the modified set...
set()

☞ union ()

In Python, the union () method is used to perform union of two sets. The union of the two sets contains the all the items that are present in both the sets.

Syntax:
set1.union (set2)

Example:
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1.union(Days2))
Output:
{'Thr', 'Fri', 'Sun', 'Tue', 'Wed', 'Mon', 'Sat'}

☞ intersection ()

In Python, the intersection () is used to calculate the intersection of the two sets in python. The intersection of the two sets is given as the set of the elements that common in both sets.

Syntax:
set1.intersection (set2)

Example:
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1.intersection(Days2))
Output:
{'Mon', 'Sat'}

☞ difference ()

The difference of two sets can be calculated by using the difference () method. The resulting set will be obtained by removing all the elements from set 1 that are present in set 2.

Syntax:
set1.difference (set2)

Example:
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1.difference(Days2))
Output:
{'Tue', 'Wed'}

☞ issubset()

The issubset() method returns True if all elements of a set are present in another set (passed as an argument). If not, it returns False.

Syntax:
set1.issubset (set2)

Example:
set1={1,2,3,4}
set2={1,2,3,4,5,6,7,8,9}
print(set1.issubset(set2))
print(set2.issubset(set1))
Output:
True
False

☞ issuperset()

The issuperset () method returns True if a set has every elements of another set (passed as an argument). If not, it returns False.

Syntax:
set1.issuperset (set2)

Example:
set1={1,2,3,4}
set2={1,2,3,4,5,6,7,8,9}
print(set1.issuperset(set2))
print(set2.issuperset(set1))
Output:
False
True

Next Topic :Dictionary sequence in Python