
Sets
In Python, sets are unordered collections of unique elements. They are useful for storing distinct values. Sets are created using curly braces {}
or the set()
constructor.
Properties of Sets
- Uniqueness: Sets do not allow duplicate elements. Each element is unique within the set.
- Unordered: Sets are unordered, meaning the elements have no specific order. So you cannot access elements by index.
- Mutable: Sets can be modified after creation, such as adding, removing, or modifying items, without needing a new structure.
- Sets can contain a mix of data types.
Example
The example below is a set named my_set
. It contains three distinct values.
my_set = {1, 2, 'hello'}print(my_set)
Output:
{1, 2, 'hello'}
Methods of Sets
Sets are mutable, meaning they allow adding and removing elements.
add()
Function
The add()
function adds an element to the set if it is not already present as sets don't allow duplicate elements.
# Create a set sample_set = {1, 2, 'hello'}
# Add an integer to the setsample_set.add(10)print(sample_set)
# Add a string to the setsample_set.add('world')print(sample_set)
Output:
{1, 2, 10, 'hello'}
{1, 2, 'hello', 10, 'world'}
It's important to note that the value "10
" appearing at different positions in the set highlights the unordered characteristic of sets. Sets do not maintain a specific order for elements, so their positions may appear random.
remove()
and discard()
Functions
Both remove()
and discard()
functions remove a specific element from the set. The difference is that if the elemnt is not found in the set, remove()
raises a KeyError while discard()
does not raise an error.
Examples of using remove()
and discard()
functions to remove existing values from a set
# Create a set sample_set = {10, 20, 30, 40}
# use remove() to remove 20 from the setsample_set.remove(20)print(sample_set)
# use discard() to remove 40 from the setsample_set.discard(40)print(sample_set)
Output:
{40, 10, 30}
{10, 30}
Example of using remove()
function to remove a non-existing value from a set
# Create a set sample_set = {10, 20, 30}
# use remove() to remove an non-existing elemenet from the set raises KeyErrorsample_set.remove(40)print(sample_set)
Output:
---------------------------------------------------------------------------KeyError Traceback (most recent call last)Input In [34], in <cell line: 5>() 2 sample_set = {10, 20, 30} 4 # use remove() to remove an unexisting elemenet from the set----> 5 sample_set.remove(40) 6 print(sample_set)
KeyError: 4
Example of using discard()
function to remove a non-existing value from a set
# Create a set sample_set = {10, 20, 30}
# use discard() to remove an non-existing elemenet from the set. No error shows upsample_set.discard(40)print(sample_set)
Output:
{10, 20, 30}
union()
Function
The union()
function returns a new set that is the union of two or more sets.
Syntax: set1.union(set2, set3, ...)
# Create two setsset1 = {1, 2, 3}set2 = {3, 4, 5}
# Union set1 and set2union_set = set1.union(set2)print(union_set)
# Create another setset3 = {'hello', 'world!'}
# Union set1, set2 and set3union_set2 = set1.union(set2, set3)print(union_set2)
Output:
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 'world!', 'hello'}
As sets don't allow duplicates, thus the union_set
only contains unique values from set1
and set2
.