Python set

Set is a collection of items that are neither ordered nor indexed. This means that we can not change the set using an index value. Set values are unique. Values can be any item such as integer, string, float, tuple, etc. Dictionary, list, set are not allowed inside the list as they are unhashable.

Creating a set

Set is creating using curly brackets ( { }  ).  Dictionary is also creating using curly brackets. The difference between set and dictionary is that set has an item like in list with single item separated by comma(, ), whereas dictionary has a key-value pair. Also, empty curly brackets represent the dictionary not set.

Example:

first_set = {1, 2, 3}
mixed_set = {"one", "two", 2, }
mixed_set_with_tuple = {"one", "two", 2, (3, 4,)}

print(type(first_set))
print(type(mixed_set))
print(type(mixed_set_with_tuple))

Output:

<class 'set'>
<class 'set'>
<class 'set'>

Selecting an item from a tuple

Since items are not indexed you can not select item in set.

Adding item to an existing set

Items can be added using .add(single_item) method or .update(single_item_iterable) method. The add() method is used to add single item and update() method is used to add multiple items.

Example:

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)

my_set.update([4, 5, 6])
print(my_set)

Output:

{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6}

Deleting item from a dictionary

Deleting item can be achieved in 3 ways

  1. using  .remove(item)  method
  2. using  .discard(item)  method 
  3. using .pop()  method

The .remove(item) removes item from the set. If the item does not exist it throws error.  The .discard(item) also removes item from a list but if the item does not exist then it will not throw error. The .pop() item removes item from last element however since set is not ordered it can remove any item. Therefore using remove(item) and discard(item) will be better option then .pop() .

Example:

my_set = {"one", "two", 2, 3, 4, 5}
my_set.remove(3)
print(my_set)

my_set.discard("two")
print(my_set)

my_set.pop()
print(my_set)

Output:

{2, 4, 5, 'two', 'one'}
{2, 4, 5, 'one'}
{4, 5, 'one'}

Note: the .pop() last output be different.

Looping through item in set

Example:

To loop through set, for loop can be used as shown below.

my_set = {"one", "two", 2, 3}
for item in my_set:
    print(item)

Output:

one
2
3
two

As you can see the output is in different order then the actual list this is because set is unordered.

Set operation

Set supports set operation like union, intersection, difference as in mathematical set. For union you can use single pipe ( | ) or .union(set_collection) method. Likewise, for intersection you can use single ampersand( & ) or .intersection(set_collection). And, difference can use minus (-) or .difference() method.   Let’s see an example:

# union using | and .union() function
a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7, 8}

u = a | b
print("union of a ,b =", u)

u = a.union(b)
print("union of a ,b =", u)

# intersection using & and .intersection() function
i = a & b
print("intersection = ", i)

i = a.intersection(b)
print("intersection = ", i)

# difference using minus(-) and .difference() method
d = a - b
print("difference d =", d)

Output:

union of a ,b = {1, 2, 3, 4, 5, 6, 7, 8}
union of a ,b = {1, 2, 3, 4, 5, 6, 7, 8}
intersection =  {4, 5}
intersection =  {4, 5}
difference d = {1, 2, 3}
difference d = {1, 2, 3}

Length of a set

Length of a set can be determined using len(iterable_item)  method.

Example:

my_set = {1,2,3}
print(len(my_set))

Output:

3

Leave a Reply

Your email address will not be published. Required fields are marked *