Previous

Set Data Type

Next

What is a Set?

 

A set is a Python class.

In Python, everything is an object.

Sets are one of the most popular data types in Python.

They contain only unique values.

Sets are unordered (the order is not preserved).

Sets are mutable, meaning we can modify the objects.

Note:

  • A set does not support indexing and slicing because the order is not preserved.

How to Create a Set Object:

Way 1:

   s1 = {10, 20, 30, 40, 50}

Way 2:

    s1 = set()

List of Set Methods:

  1. add(value)
  2. remove(value)
  3. discard(value)
  4. pop()
  5. copy()
  6. clear()
  7. s1.union(s2)
  8. s1.intersection(s2)
  9. s1.difference(s2)
  10. s1.issubset(s2)
  11. s1.issuperset(s2)
  12. s1.isdisjoint(s2)
  13. s1.intersection_update(s2)
  14. s1.difference_update(s2)
  15. s1.update(s2)
  16. s1.symmetric_difference(s2)
  17. s1.symmetric_difference_update(s2)

Details Explanation about all the set method

  1. add(value)

This is a method of a Set object. It takes a single argument, value, which will be added to the set. There is no guarantee of order or index for the elements in the set, as sets are unordered collections.

Example :

def set_add(s1, value):
    s1.add(value)
    return s1

s1 = {'ten', '20', 'binary' , 'bridge'}
print('Existing Set: ', s1)

value = input('Enter the value: ')
print('set with added value: ', set_add(s1, value))

Output

Existing Set:  {'ten', 'bridge', 'binary', '20'}
Enter the value: 'tech'
set with added value:  {'ten', "'tech'", 'bridge', '20', 'binary'}

2. remove(value)
This is a method of a Set object that takes a single argument, value. The remove method is used to remove the specified value from the set. If the value exists in the set, it will be removed without returning anything. If the value does not exist in the set, a KeyError will be raised.

Example :

def set_remove(s1, value):
    s1.remove(value)
    return s1

s1 = {'tech', '20', 'binary' , 'bridge'}
print('Existing Set: ', s1)

value = input('Enter the value: ')
print('set with removed value: ', set_remove(s1, value))

Output

Existing Set:  {'tech', 'bridge', '20', 'binary'}
Enter the value: 20
set with removed value:  {'tech', 'bridge', 'binary'}

 

3. discard(value)

This method is similar to the remove(value) method, but it does not raise an error if the specified value does not exist in the set. If the value exists, it will be removed from the set; if it does not exist, the method will simply do nothing.

Example :

def set_discard(s1, value):
    s1.discard(value)
    return s1

s1 = {'tech', '20', 'binary' , 'bridge'}
print('Existing Set: ', s1)

value = input('Enter the value: ')
print('set with removed value: ', set_discard(s1, value))

 

Output - 1

Existing Set:  {'tech', 'bridge', '20', 'binary'}
Enter the value: "value not exits"
set with removed value:  {'tech', 'bridge', '20', 'binary'}

Output - 2

Existing Set:  {'bridge', 'binary', '20', 'tech'}
Enter the value: 20
set with removed value:  {'bridge', 'binary', 'tech'}

4. pop()

The pop() method removes an arbitrary item from a set object and does not return anything. It raises a KeyError if the set object is empty.

Example :

def set_pop(s1):
    s1.pop()
    return s1

s1 = {'tech', '20', 'binary' , 'bridge'}
print('Existing Set: ', s1)

print('set with removed value: ', set_pop(s1))

Output :

Existing Set:  {'binary', 'tech', 'bridge', '20'}
set with removed value:  {'tech', 'bridge', '20'}

5. clear()

The clear() method removes all items from a set, leaving it empty. It does not return any value.

Example :

def set_clear(s1):
    s1.clear()
    return s1
s1 = {'tech', '20', 'binary' , 'bridge'}
print('Existing Set: ', s1)

print('set with removed value: ', set_clear(s1))

 

Output :

Existing Set:  {'bridge', '20', 'tech', 'binary'}
set with removed value:  set()

6. s1.union(s2)

The union() method returns a new set containing all unique values from both set1 and set2.

Example :

def set_union(s1, s2):
    return s1.union(s2)

s1 = {'tech', '20', 'binary' , 'bridge'}
s2 = {'ram', '20', 'python' , '10'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('set with union: ', set_union(s1,s2))

 

Output :

Existing Set1:  {'bridge', 'tech', 'binary', '20'}
Existing Set2:  {'10', 'ram', '20', 'python'}
set with union:  {'binary', '10', 'tech', '20', 'ram', 'bridge', 'python'}

7. s1.intersection(s2)

The intersection() method returns a new set containing only the common values from both set1 and set2.

Example :

def set_intersection(s1,s2):
    return s1.intersection(s2)

s1 = {'tech', '20', 'binary' , 'bridge'}
s2 = {'ram', '20', 'python' , '10'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('set with union: ', set_intersection(s1,s2))

Output :

Existing Set1:  {'20', 'tech', 'bridge', 'binary'}
Existing Set2:  {'20', 'python', 'ram', '10'}
set with union:  {'20'}

8. s1.difference(s2)

The difference() method returns a new set containing the values that are in set1 but not in set2.

Example - 1 :

def set_difference(s1,s2):
    return s1.difference(s2)

s1 = {'tech', '20', 'binary' , 'bridge'}
s2 = {'ram', '20', 'python' , '10'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_difference(s1,s2))

Example - 2 :

def set_difference(s1,s2):
    return s2.difference(s1)

s1 = {'tech', '20', 'binary' , 'bridge'}
s2 = {'ram', '20', 'python' , '10'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_difference(s1,s2))

Output - 1 :

Existing Set1:  {'binary', 'tech', 'bridge', '20'}
Existing Set2:  {'ram', '10', '20', 'python'}
Answer :  {'binary', 'tech', 'bridge'}

Output - 2 :

Existing Set1:  {'binary', 'tech', 'bridge', '20'}
Existing Set2:  {'ram', 'python', '10', '20'}
Answer :  {'ram', 'python', '10'}

 

9. s1.issubset(s2):

This method returns True if all elements of set s1 are also in set s2. If s1 has any elements not in s2, it returns False

Example - 1 :

def set_issubset(s1,s2):
    return s2.issubset(s1)

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'best', 'learning', 'portal'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_issubset(s1,s2))

Example - 2 :

def set_issubset(s1,s2):
    return s1.issubset(s2)

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'best', 'learning', 'portal'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_issubset(s1,s2))

 

Output - 1 :

Existing Set1:  {'bridge', 'binary', 'learning', 'portal', 'best', 'tech'}
Existing Set2:  {'learning', 'portal', 'best'}
Answer :  True

Output - 2 :

Existing Set1:  {'bridge', 'binary', 'learning', 'tech', 'portal', 'best'}
Existing Set2:  {'learning', 'portal', 'best'}
Answer :  False

10. s1.issuperset(s2):

This method returns True if set s1 contains all elements of set s2. If s2 has any elements not in s1, it returns False.

Example - 1 :

def set_issuperset(s1,s2):
    return s1.issuperset(s2)

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'best', 'learning', 'portal'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_issuperset(s1,s2))

Example - 2 :

def set_issuperset(s1,s2):
    return s2.issuperset(s1)

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'best', 'learning', 'portal'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_issuperset(s1,s2))

Output - 1 :

Existing Set1:  {'learning', 'bridge', 'best', 'tech', 'portal', 'binary'}
Existing Set2:  {'learning', 'portal', 'best'}
Answer :  True

Output - 2 :

Existing Set1:  {'best', 'bridge', 'learning', 'tech', 'binary', 'portal'}
Existing Set2:  {'best', 'learning', 'portal'}
Answer :  False

11. s1.isdisjoint(s2)

This method returns True if the two sets, s1 and s2, have no elements in common (i.e., they are disjoint). If there is at least one element that appears in both sets, it returns False.

Example - 1 :

def set_isdisjoint(s1,s2):
    return s1.isdisjoint(s2)

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'Across', 'World'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_isdisjoint(s1,s2))

Example - 2 :

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'Across', 'World', 'bridge'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_isdisjoint(s1,s2))

Output - 1 :

Existing Set1:  {'binary', 'bridge', 'tech', 'learning', 'best', 'portal'}
Existing Set2:  {'World', 'Across'}
Answer :  True

Output - 2 :

Existing Set1:  {'binary', 'best', 'bridge', 'tech', 'learning', 'portal'}
Existing Set2:  {'Across', 'bridge', 'World'}
Answer :  False

12. s1.intersection_update(s2)

This method updates the set s1 in place to keep only the elements that are also in s2. It does not create a new set; it modifies s1 directly.

Example - 1 :

def set_intersection_update(s1,s2):
    s1.intersection_update(s2)
    print('"intersection_update : "Id of s1 ', id(s1))
    return s1

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'Across', 'World', 'bridge'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)
print('Id of s1 ', id(s1))

print('Answer : ', set_intersection_update(s1,s2))

 Output - 1 :

Existing Set1:  {'learning', 'best', 'portal', 'tech', 'binary', 'bridge'}
Existing Set2:  {'Across', 'World', 'bridge'}
Id of s1  140506038986112
"intersection_update : "Id of s1  140506038986112
Answer :  {'bridge'}

Note : Here Id of s1 are same .

Example - 2 :

def set_intersection_update_without(s1,s2):
    # without using intersection_update() method
    s1 = s1.intersection(s2)
    print('"intersection_update : "Id of s1 ', id(s1))
    return s1

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'Across', 'World', 'bridge'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)
print('Id of s1 ', id(s1))

print('Answer : ', set_intersection_update_without(s1,s2))

 

 Output - 2 :

Existing Set1:  {'best', 'learning', 'portal', 'bridge', 'tech', 'binary'}
Existing Set2:  {'World', 'bridge', 'Across'}
Id of s1  139880205006208
"intersection_update : "Id of s1  139880205006880
Answer :  {'bridge'}

Note : Here Id of s1 is not same .

13. s1.difference_update(s2)

This method is similar to the difference() method, but instead of returning a new set, it modifies the set s1 in place. It removes all elements from s1 that are also present in s2, effectively updating s1 to contain only the elements that are unique to it.

Example - 1 :

def set_difference_update(s1,s2):
    s1.difference_update(s2)
    return s1

s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'Across', 'World', 'bridge'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_difference_update(s1,s2))

 Output - 1 :

Existing Set1:  {'learning', 'tech', 'best', 'binary', 'bridge', 'portal'}
Existing Set2:  {'World', 'bridge', 'Across'}
Answer :  {'learning', 'tech', 'best', 'binary', 'portal'}

14. s1.update(s2)

This method is similar to the union() method, but instead of returning a new set, it modifies the set s1 in place. It adds all elements from s2 to s1, ensuring that s1 contains all unique elements from both sets.

Example - 1 :

def set_update(s1,s2):
    s1.update(s2)
    return s1
s1 = {'binary', 'bridge', 'tech', 'best', 'learning', 'portal'}
s2 = {'Across', 'World', 'bridge'}
print('Existing Set1: ', s1)
print('Existing Set2: ', s2)

print('Answer : ', set_update(s1,s2))

 Output - 1 :

Existing Set1:  {'learning', 'portal', 'tech', 'best', 'binary', 'bridge'}
Existing Set2:  {'World', 'Across', 'bridge'}
Answer :  {'World', 'learning', 'portal', 'tech', 'best', 'Across', 'binary', 'bridge'}

 

15. s1.symmetric_difference(s2)

Coming Soon

Example - 1 :

 

Output - 1 :

 

16. s1.symmetric_difference_update(s2)

Coming Soon

Example - 1 :

 

Output - 1 :