Python Set
By: Daniel Liu
Tags: Data Structures, Python, Set, Python Set, Python Programming

Overview

A set is a collection of unique items that are unordered and unindexed. This means that there are no duplicates and cannot access items as you would a list or dictionary. Once you add an item into the set it cannot be changed but you can remove and add a new item.

Create

To create a set you can use curly braces or use the set constructor.

newset1 = {"one", "two", "three"}

newset2 = set(("one", "two", "three")) print(newset1 == newset2)

>> True

Insert

To add an item using the add() method.

newset = {"one", "two", "three"}

newset.add("four")

print(newset)

>> {'one', 'four', 'three', 'two'}

To add multiple items use the update() method.

newset = {"one", "two", "three"}

newset.update(["five", "six"])

print(newset)

>> {'five', 'six', 'one', 'two', 'three'}

If you try to add a duplicate item using the add() or update() method you will not get an error but the duplicate will not be in the set.

Read

To access items in the set you can loop through the set.

newset = {"one", "two", "three"}

for num in newset:

print(num)

>> one

>> two

>> three

To check if an item is in the set use the in keyword.

newset = {"one", "two", "three"}

print("one" in newset)

>> True

print("four" in newset)

>> False

Delete

To remove a specific item from the set you can use the remove() method. It will throw an error if the item is not in the set.

newset = {"one", "two", "three"}

newset.remove("one")

print(newset)

>> {'two', 'three'}

Similarly you can use the discard() method to remove a specific element as well but it will not throw an error if it is not in the set.

newset = {"one", "two", "three"}

newset.discard("one")

print(newset)

>> {'two', 'three'}

To remove the last item in the set you can use the pop() method. The pop() method will also return the item that was removed from the set.However, sets are unordered so the item might not be what you expect.

newset = {"one", "two", "three"}

x = newset.pop()

print(x)

>> two

print(newset)

>> {'one', 'three'}

To remove all items from the set you can use the clear() method.

newset = {"one", "two", "three"}

newset.clear()

print(newset)

>> set()

To remove the set itself you can use the del keyword

newset = {"one", "two", "three"}

del newset

# Error will be thrown because newset does not exist anymore

print(newset)

Length

To get the length of the set you can use the len() method.

newset = {"one", "two", "three"}

print(len(newset))

>> 3

Joining Sets

To join two sets you can use the union() method which will return a new set.

newset1 = {"one", "two", "three"}

newset2 = {1, 2, 3}

print(newset1.union(newset2))

>> {1, 'one', 2, 3, 'two', 'three'}

You can use the update() method to items from one set into another.

newset1 = {"one", "two", "three"}

newset2 = {1, 2, 3}

newset1.update(newset2)

print(newset1)

>> {1, 2, 3, 'three', 'two', 'one'}

Conclusion

Like other languages set is a data structure that is unordered, unindexed and has no duplicates. Items in sets are hashed so it is O(1) for inserting, reading and removing. If you do not care about duplicates and order you would want to use a set.