Set

Python has a built-in method called set. set type has the following characteristics

  • Sets are a collection which is unordered and unindexed

  • Set elements are unique. Duplicate elements are not allowed.

  • A set itself may be mutable, but the elements within a set is immutable.

You can create sets in two ways:

  1. using set method followed by a parenthesis ().

  2. using curly brackets {}.

set()

You can have an ITERABLE object such as list or tuple within set(<iter>). This returns the list or tuple as a set wrapped in a curly bracket {}. Any iterable object can be converted to a set using set(). You can think of set() as extend() method of lists.

A list within set().

numbers = set([1, 2, 3, 4, 5, 6, 7])

print(numbers)

A tuple within set().

numbers = set((1, 2, 3, 4, 5, 6, 7))

print(numbers)

A string with a set().

my_letters = set('ABCDEF')

print(my_letters)

While converting an iterable objects to a set, the returned set is deduplicated.

# Example 1
my_cities = set(['Krakow', 'Warsaw', 'Warsaw', 'Kielce'])

print(my_cities)


# Example 2
my_letters = set('AaBBCCDDEEE')

print(my_letters)


# Example 3
my_numbers = set('12345342')

print(my_numbers)

You can see that the output are unordered and deduplicated. The original orders are not kept. set() only accepts an object that is iterable such as a string, list or tuple. For example, integers are not iterable and it raises an error, to be specific TypeError, while we try to create a set with integer.

my_numbers = set(12345342)

print(my_numbers)

curly bracket {}

You can create a set using curly brackets {}. Curly brackets {} must have only IMMUTABLE objects. Each element has to separated by a comma, similar to lists and tuples, in other words, a set can be created as {<obj1>, <obj2>, <obj3>, ......, <objn>}.

# Example 1
my_cities = {'Krakow', 'Warsaw', 'Warsaw', 'Kielce'}

print(my_cities)


# Example 2
my_letters = {'AaBBCCDDEEE'}

print(my_letters)


# Example 3
my_numbers = {12345342}

print(my_numbers)

As you can see, the curly brackets do not iterate through iterable elements. Each object is present in the set intact regardless of iterability.

Empty set

set can also be empty, as we had empty list and empty tuple. You can create an empty set using built-in function of set() only because Python interprets empty curly brackets {} as an empty dictionary.

empty_set = set()

# Check empty_set type
print(type(empty_set))

print(empty_set)

Mixed datatypes set

A set can have a mixed datatypes

# set function
mixed_set = set([34, 3.2, 'cat', 1.858, False, True, 'Name'])

print(mixed_set)

# Curly brackets
mixed_set_curly = {34, 3.2, 'cat', 1.858, False, True, 'Name'}

print(mixed_set_curly)

How to add element(s) to a set?

Sets are unordered and changing with indexing brackets is not possible. Sets are mutable, but we cannot perform slicing or indexing operations to access its elements. Python raises TypeError when you use indexing or slicing operation.

number_set = {1, 2, 3, 4}

print(number_set[:2])

You can use set method of add()to add an element. add() method can be used to add an element, it takes only an arguments (add(<obj>)).

new_set = {9, 8, 7, 6}

new_set.add(5)

print(new_set)

You can use set method update() to add elements . update() requires an iterable datatype (simple or complex) (update(<iter>)).

new_set = {9, 8, 7, 6}

new_set.update([5, 2, 4, 3])

print(new_set)

How to delete element(s) from a set?

You can delete an element from a set using discard() or remove().

remove function

remove() will delete the element where it is present and raises a KeyError where the element is absent.

# Element is present
new_set = {9, 8, 7, 6}

new_set.remove(8)
print(new_set)


# Element is absent
new_set = {9, 8, 7, 6}

new_set.remove(5)
print(new_set)

discard function

You can also use discard() to delete an element. if element is a member of the set, then removes it, but it does nothing when element is not a member of a set.

# Element is present
new_set = {9, 8, 7, 6}

new_set.discard(8)
print(new_set)


# Element is absent
new_set = {9, 8, 7, 6}

new_set.discard(5)
print(new_set)

pop function

You can use pop() on a set. pop() returns an arbitrary element because sets are unordered.

new_set = {9, 8, 5, 4, 7, 6}

print(new_set.pop())
print(new_set)

Set methods and operators

You can use Python set methods and operators to perform operations such as union, intersection, difference and symmetric difference.

union

The set made by combining the elements of two sets.

You can use union() method or | operator.

set_1 = {4, 5, 6, 7, 8, 9}
set_2 = {1, 4, 3, 5, 6}

# method 1
new_set = set_1.union(set_2)
print(new_set)


# method 2
new_set_2 = set_1 | set_2
print(new_set_2)

| operator creates a union of two sets (both side have to be sets), otherwise, it raises an error. While union() takes an iterable and converts it to a set before performing union operation. See example below; notice that the second set is a tuple.

set_1 = {4, 5, 6, 7, 8, 9}
set_2 = (1, 4, 3, 5, 6)

# method 1
new_set = set_1.union(set_2)
print(new_set)


# method 2
print(set_1 | set_2)

As you can see, the union runs successfully but | operator raises TypeError.

intersection

set intersection is the elements that are only in both sets or the elements which are overlapping.

You can use intersection() method or & operator to get intersect of two sets.

set_1 = {4, 5, 6, 7, 8, 9}
set_2 = {1, 4, 3, 5, 6}

# method 1
new_set = set_1.intersection(set_2)
print(new_set)


# method 2
new_set_2 = set_1 & set_2
print(new_set_2)

difference

You can use difference() method or - operator to get intersect of two sets. The difference of set A and set B is a set of elements that are only present in set A but not set B. The difference of set B and set A is vice versa.

set 1 difference

set_1 = {4, 5, 6, 7, 8, 9}
set_2 = {1, 4, 3, 5, 6}

# method 1
set_1_diff = set_1.difference(set_2)
print(set_1_diff)


# method 2
set_1_diff_op = set_1 - set_2
print(set_1_diff_op)

set 2 difference

set_1 = {4, 5, 6, 7, 8, 9}
set_2 = {1, 4, 3, 5, 6}

# method 1
set_2_diff = set_2.difference(set_1)
print(set_2_diff)


# method 2
set_2_diff_op = set_2 - set_1
print(set_2_diff_op)

symmetric difference

symmetric difference is a set that contains all the elements from set A and set B that is not shared. It can be seen as opposite of intersection.

You can You can use symmetric_difference() method or ^ operator.

set_1 = {4, 5, 6, 7, 8, 9}
set_2 = {1, 4, 3, 5, 6}

# method 1
sym_diff = set_1.symmetric_difference(set_2)
print(sym_diff)


# method 2
sym_diff_op = set_1 ^ set_2
print(sym_diff_op)

All set methods and operators above support multiple set union, intersection, difference and symmetric difference when you are using methods and operators except symmetric difference method.

set_1 = {4, 5, 6, 7, 8, 9}
set_2 = {1, 4, 3, 5, 6}
set_3 = {1, 5, 6, 10}

# method 1
sym_diff_op = set_1 ^ set_2 ^ set_3
print(sym_diff_op)


# method 2
sym_diff = set_1.symmetric_difference(set_2, set_3)
print(sym_diff)

Last updated