5️⃣ Set

  • Set can store multiple unique items in a single variable.

  • It is unordered collection of items, that’s why those are also called non sequential datatypes.

  • It is defined with curly brackets { }.

  • Set can contain items of different datatypes.

💻 Example:

1
2
myset = {1,2,3, "apple"}
print(type(myset))

✅ Output:

<class 'set'>

Set Methods

👉🏻1) len():

It is builtin function which returns how many items present in a set.

💻 Example:

1
2
myset = {'1', '2', 'abc'}
print(len(myset))

✅ Output:

3

👉🏻2) .add():

This method adds an item to a set.

💻 Example:

1
2
3
myset = {'1','2', 'abc'}
myset.add("xyz")
print(myset)

✅ Output:

{'abc', '2', '1', 'xyz'}

👉🏻3) .update():

We can add items from another set into the current set.

💻 Example:

1
2
3
4
5
myset = { "apple", "chikoo", "cherry"}
thisset= {1, 2, 3}
myset.update(thisset)
print(myset)
print(thisset)

✅ Output:

{'apple', 1, 2, 'chikoo', 'cherry', 3}
{1, 2, 3}

👉🏻4) .remove():

  • We can use .remove() or .discard() method to remove an item from a set.

  • If we want to remove an element which is not present in set using .remove method will provide KeyError.

  • The .discard() method won’t provide KeyError for the same case.

💻 Example:

1
2
3
4
5
6
myset = { "abc", "bcd", "efg", "hij"}
myset.remove('bcd')
print(myset)
myset.remove('aaa')
myset.discard('aaa')
print(myset)

✅ Output:

{'abc', 'efg', 'hij'}
Traceback (most recent call last):
    File "<pyshell#27>", line 1, in <module>
      myset.remove('aaa')
  KeyError: 'aaa'
{'abc', 'efg', 'hij'}

👉🏻5) .clear():

This method clears all elements present in set and makes the current set empty.

💻 Example:

1
2
3
myset = {1, 2, 3, 4}
myset.clear()
print(myset)

✅ Output:

set()

👉🏻6) del:

It is keyword, which is used to completely delete a variable from memory.

💻 Example:

1
2
3
# delete myset from memory
del myset
print(myset)

✅ Output:

 Traceback (most recent call last):
   File "<pyshell#34>", line 1, in <module>
    myset
NameError: name 'myset' is not defined

Set Operations

There are several ways to join two or more sets in Python using below operations.

👉🏻1) union():

This method combines the elements from two sets. The new set formed using union method will always contains unique elements.

💻 Example:

1
2
3
4
set1= {"a", "b", "c"}
set2 = {1,2,3,'b','c'}
set3 = set1.union(set2)
print(set3)

✅ Output:

{1, 2, 3, 'a', 'b', 'c'}

👉🏻2) intersection():

This method will provide only common items that are present in both sets.

💻 Example:

1
2
3
4
setA= {"a","b","c"}
setB = {"b",1, 2}
setC = setA.intersection(setB)
print(setC)

✅ Output:

{'b'}

👉🏻3) intersection_update():

This method will also keep only common items that are present in both sets and update the result in first set.

💻 Example:

1
2
3
4
5
6
setA= {"a","b","c"}
setB = {"b",1, 2}
setA.intersection_update(setB)
# setA will be updated with the intersection result
print(setA)
print(setB)

✅ Output:

{'b'}
{"b",1, 2}

💻 Example:

1
2
3
4
5
6
setA= {"a","b","c"}
setB = {"b",1, 2}
setB.intersection_update(setA)
# setB will be updated with the intersection result
print(setA)
print(setB)

✅ Output:

{"a","b","c"}
{'b'}

👉🏻4) difference():

It will return a new set, that contains only the elements that are present in one set but not present in other set.

💻 Example:

1
2
3
4
5
6
7
8
9
x = { "apple","banana", "cherry"}
y = { "google", "microsoft", "apple"}
a = x.difference(y)
b = y.difference(x)

print("Elements present in set x but not in set y are:")
print(a)
print("Elements present in set y but not in set x are:")
print(b)

✅ Output:

Elements present in set x but not in set y are:
{'banana', 'cherry'}
Elements present in set y but not in set x are:
{'microsoft', 'google'}

👉🏻5) symmetric_difference():

It will return a new set, that contains only the elements that are not present in both the sets.

💻 Example:

1
2
3
4
x = { "apple","banana", "cherry"}
y = { "google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)

✅ Output:

{'cherry', 'banana', 'microsoft', 'google'}

Be prepared with below questions prior to your interview !

Frequently asked Interview Questions
  1. What is Python set?

  2. How do you create a set in Python?

  3. What is the difference between Python set and list?

  4. Can a set contain duplicate elements?

  5. How do you add elements to a set?

  6. Can you modify elements in a set?

  7. How do you remove elements from a set?

  8. What operations can you perform on sets?

  9. How do you perform set intersection and union in Python?

  10. Can you convert a list or tuple into a set?