Member-only story
10 things you should know about Sets in Python
Guidelines to use sets in Python
1. What is a Python set?
A set is an unordered and mutable collection of unique elements. Sets are written with curly brackets ({}), being the elements separated by commas.
The following code block shows two sets, containing a collection of numbers and cities.
Any immutable data type can be an element of a set (e.g. strings and integers). If you try to use a mutable data type inside a set, an exception (TypeError) is raised, as shown below.
2. Create a set with set() constructor
Sets can also be defined with the built-in function set([iterable]). This function takes as argument an iterable (i.e. any type of sequence…