Python Set Data Structure With Examples – POFTUT

Python Set Data Structure With Examples


Python provides different structures to hold data. We have all ready looked list and dictionary in this site. Today we will look usage examples and features of python set.

Set have same properties and behaviour of mathematical set. Sets have following features

  • unordered means the order is not important and do not expect same order for all operations
  • no duplicate means the items in a set will be unique so adding same item will no effect on the set.

Create Set

Set’s in python can be created in different ways. We will look the simplest way. We will use curly brackets { } and put items into these brackets by separating them with comma , . The type of the elements can be anything like string, int, object.

In this example we will create a set named myset and put some element into it.

myset={1,2,3,4}

Add Element To Set

In previous part we have defined new python set. But defining the set is not enough for real world problems. We will need to add more items into set. We can add new item into set by using add function and providing the item as parameter to this function.

In this example we will add 5 in to the set by using add function.

myset.add(5)

Add Multiple Elements

We can also add multiple elements into an existing set too. We will use update function in order to add multiple elements. We will provide multiple elements in a list like format.

In this example we will add new items 6,7,8 into set named myset .

myset.update([6,7,8])
Add Multiple Elements
Add Multiple Elements

Remove Element From Set

Element removal done in sets with two functions named discard and remove . The different is that while using remove if the element do not exist in the set an error will be raised but in discard usage there will be no error or any output about the operation.

LEARN MORE  Dymerge Dictionary Merge Tool

In this example we will remove 1 from set named myset .

myset.discard(1)

OR

myset.remove(2)

and we can try to remove unexist element 1

myset.remove(1)

Clear Set

Removing elements from set one by one may be sometimes very trivial job. Set provides clear function where all elements will be removed at once.

myset.clear()
Clear Set
Clear Set

Set Operations

Set’s have some unique features and operations in mathematic. We can use these operations in python too. Python set supports following set operations where they will be examined later in this tutorial in detail.

  • Union
  • Intersection
  • Difference
  • Symmetric Difference

Union Two Sets

We can union two set and create a cumulative but uniq new set. We will use | as union operation for two sets.

In this example we have two sets named myset and yourset . We will union these sets and print into console. We can also assign new unin set into a variable which will be a set.

unionset = myset | yourset
Union Two Sets
Union Two Sets

Intersection Of Two Sets

Another operation of the set is intersection. Intersection operations will create new set where its elements consist of two sets common elements. If an element exists in both set this element will be put into intersection set.

intersectionset = myset & yourset
Intersection Of Two Sets
Intersection Of Two Sets

Difference Of Two Sets

Difference operation is used to identify first set uncommon elements. Common elements will be eliminated after difference operation and uncommon elements will be put into new set. In this example we will get difference from myset to yourset .

differenceset = myset - yourset
Difference Of Two Sets
Difference Of Two Sets

Or we can use difference function like below where it will provides same result as above.

differenceset = myset.difference(yourset)

Symmetric Difference Of Two Sets

Symmetric difference will select both sets uncommon elements and create new set for them. Common elements will not put into this new set. Symmetric difference is reverse operation of intersection. We will use ^ as symmetric difference operation.

symmetricset = myset ^ yourset
Symmetric Difference Of Two Sets
Symmetric Difference Of Two Sets

Loop/Enumerate In Set Elements

Looping in sets is very popular case for programmers. Set is enumerable like other structures list, sequence, dictionary etc. We can use for to loop in set elements.

LEARN MORE  Powershell Array Tutorial with Examples

In this example we will loop in set named myset and print its elements one by one.

myset = {3, 4, 5, 6, 7, 8}

for element in myset:
   print(element)
Loop/Enumerate In Set Elements
Loop/Enumerate In Set Elements

Leave a Comment