The Python set is an unordered list of items which are unindexed. Python Sets allows immutable items, it means only unique values (no duplicates are allowed).

Generally, Python sets are very useful to perform mathematical operations like Union, Intersections, set comparisons, and set differences.

Sets allows you to place different kind of data type items like integer, string etc in a single set. The following are the available list of ways to declare sets.

# Python set Declaration

# Empty set
mySet1 = {}
mySet2 = set()

# Set with Integer Keys
mySet3 = {1, 2, 3, 4, 5}

# Set with String Keys
mySet4 = {'apple', 'kiwi', 'banana', 'orange'}

print(mySet1)
print(mySet2)
print(mySet3)
print(mySet4)

OUTPUT

Python Set Example 1

Python Mixed Sets Example

As we mentioned earlier, we can create a set with multiple data types. In this example, we declare a set with mixed data types. Next, we use the set() to declare a set

# Python set Declaration

# Set with Mixed Data Types
mySet1 = {1, 2, 1.5, 2.5, 'apple'}
mySet2 = {'banana', 1, 2, (1, 2, 3)}

print(mySet1)
print(mySet2)

# Python Set using set()
mySet3 = set([1, 2, 3, 4, 5])
mySet4 = set((1, 2, 3, 4, 5, 6, 7))

print(mySet3)
print(mySet4)

# set() example 2
mySet5 = (['apple', 'kiwi', 'banana', 'orange'])
mySet6 = (('apple', 'kiwi', 'banana', 'orange'))

print(mySet5)
print(mySet6)

OUTPUT

Python Set Example 2

Python set {} vs set()

This example show you the difference between {} and set() while declaring a set.

Python set Example 4

Convert Dictionary to Set in Python

If you use the above technique to convert a Dictionary to Set then it will throw an error. You can see the same in the below example.

# Python set Declaration

# Dictionary to Set
mySet1 = {'a':1, 'b':2, 'c':3}
print(mySet1)
print({mySet1})

OUTPUT

Python set Example 5

Insert items into Python Set

Python provides the following options to insert new items into the existing Set.

Python set Example 8

Remove Python Set items

There are several different functions to delete or remove an item from a Set:

Remove Python Set item using remove() method

If we know the set item or the value inside the given set then, we can use remove method.

# Python set remove

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Old Set Items = ", mySet)

# Set remove()
mySet.remove(4)
print("New Set Items = ", mySet)

FruitSet = {'apple', 'Mango', 'orange', 'banana', 'cherry','kiwi'}
print("\nOld Set Items = ", FruitSet)

# Set remove()
FruitSet.remove('orange')
print("New Set Items = ", FruitSet)

OUTPUT

Python Set 9

From the above screenshot you can observe that,

  • mySet.remove(4) removed 4 from mySet. After removing, remaining set items adjusted themselves.
  • FruitSet.remove(‘orange’) removed orange from fruits set.

Remove Python set item using del statement

The Del statement completely removes the given set. If you select the set after executing this del method, it will throw an error. This is because, this function will completely remove the set.

# Python set del

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Old Set Items = ", mySet)

# Set del
del mySet
print("New Set Items = ", mySet)

OUTPUT

Python set Example 13

Remove Python set items using clear method

The set clear method in help you to clear all the items in given set. After executing this method, it returns an empty set as set().

# Python set clear

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Old Set Items = ", mySet)

# Set clear()
mySet.clear()
print("New Set Items = ", mySet)

FruitSet = {'apple', 'Mango', 'orange', 'banana', 'cherry','kiwi'}
print("\nOld Set Items = ", FruitSet)

# Set clear()
FruitSet.clear()
print("New Set Items = ", FruitSet)

OUTPUT

Remove Python Set item using discard() method

The discard method remove the user specified set item from a given set. If we know the item or value that you want to delete then, you can use python discard to remove the given set item.

In python, Discard and Remove functions are same. However, if you try to remove a non existing item then Remove with raise an error, and discard will not raise any error.

# Python set discard

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Old Set Items = ", mySet)

# Set discard()
mySet.discard(7)
mySet.discard(4)
print("New Set Items = ", mySet)

FruitSet = {'apple', 'Mango', 'orange', 'banana', 'cherry','kiwi'}
print("\nOld Set Items = ", FruitSet)

# Set discard()
FruitSet.discard('Mango')
print("New Set Items = ", FruitSet)

OUTPUT

Python set Example 10

From the above screenshot you can observe that,

  • First, we declared a set of integer values.
  • mySet.discard(7) removing 7 from mySet.
  • In the Second statement, mySet.discard(4) removing 4 from mySet.
  • Next, we declared a fruit set contains 6 fruit names.
  • FruitSet.discard(‘Mango’) removing Mango from fruit set.

Remove set items using pop() method

The pop method remove the last item from a given set. As we all know, sets are not in any particular order. So, you never know, which item was removed from the set.

Although, it is not worth, I am giving an example. So that, you get the idea.

# Python set discard

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Old Set Items = ", mySet)

# Set discard()
mySet.discard(7)
mySet.discard(4)
print("New Set Items = ", mySet)

FruitSet = {'apple', 'Mango', 'orange', 'banana', 'cherry','kiwi'}
print("\nOld Set Items = ", FruitSet)

# Set discard()
FruitSet.discard('Mango')
print("New Set Items = ", FruitSet)

OUTPUT

Python set pop 11

Thank You for Visiting Our Blog