Python Set (With Examples)

Python Set is an “unordered collection of elements or items”. Every element in the Set is unique means no duplicates are allowed and must be immutable, which means it cannot be changed in the future.

Creating a Set

To create a Set, you can use the built-in function “set()”. It is formed by placing all the items inside the curly braces {}, and a comma separates them.

The Set can have any number of items, which may be of different types (integer, float, tuple, string, etc.). But the Set cannot have a mutable element, like a list or dictionary, as its element.

Example

setA = {21, 'AppDividend', (21, 22, 19)}

print(setA)

Output

Getting Started With Sets

Set constructor

You can also use the set() constructor to create a set.

setData = set(("data", "base", "fire", "base"))

print(setData)

Output

{'data', 'fire', 'base'}

Accessing elements of a Set

To access elements of a set, loop through it.

You cannot access items in the Set by referring to an index. Since sets are unordered, the items have no index. However, you can search for the specified value in a set by using them in the keyword.

Example

setA = {21, 'AppDividend', (21, 22, 19)}

for item in setA:
  print(item)

Output

Access Items in Python Sets

Creating an Empty Set

To create an empty set, use the “empty curly braces” {}.

# create an empty set
empty_set = set()

print("The empty set", empty_set)

# check data type of empty_set
print('Data type of empty_set:', type(empty_set))

Output

The empty set set()
Data type of empty_set: <class 'set'>

Adding elements in Set

The Set is mutable. But since they are unordered, indexing has no meaning.

To add one item to a set, we can use the add() method. If we wish to add more than one item to a set using the update() method.

Example

setA = {21, 'AppDividend', (21, 22, 19)}

setA.add(True)

print(setA)

In the above example, we have added one item, the Boolean type. We can use the update() method to add multiple items to the Set.

setA = {'Bad Bunny', 'Drake'}

setA.update({'Daddy Yankee', 'Louis Fonsi'})

print(setA)

Output

Add Items In Python Set

Updating a Set

The update() method is “used to update the set with elements from other collection types (lists, tuples, sets, etc)”.

Example

fashion_companies = {'Louis Vuitton', 'Dior', 'Gucci'}
indian_companies = ['Zara', 'Zudio', 'Jockey']

fashion_companies.update(indian_companies)
print(fashion_companies)

Output

{'Zudio', 'Zara', 'Gucci', 'Dior', 'Jockey', 'Louis Vuitton'}

Getting the length of a Set

You can use the “len()” function to determine the length of the Set.

setA = {'Bad Bunny', 'Drake'}

setA.update({'Daddy Yankee', 'Louis Fonsi'})

print(len(setA))

Output

Get the Length of a Set

Removing an element from Set

We cannot access or change an item of the Set using indexing or slicing.

We can remove the item from the Set by specifying the item value.

setA = {'Bad Bunny', 'Louis Fonsi', 'Daddy Yankee'}

setA.remove('Louis Fonsi')

print(setA)

Output

Remove Item From Set

If an item to remove does not exist, the remove() method will raise an error.

Built-in Functions with Set

Function Description
add() Adds an element to the set
clear() Removes all elements from the set
copy() Returns a shallow copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another specified set
discard() Removes a specified element.
intersection() Returns a set that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other specified set(s)
isdisjoint() Returns whether two sets have an intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() Inserts the symmetric differences from this set and another
union() Returns a set containing the union of sets
update() Updates the set with the union of this set and others

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.