Python Data Structures — Set

Renu Khandelwal
Sep 8, 2018 · 5 min read

Prerequisites : Basic knowledge of any programming language

we will explore Data Structure Set and all its operations here.

Jupyter notebook for Set :

For all operation on List

For all operation on Dictionary

For all operation on Tuple

For common DataFrame

Here we will know about what is a set, how to create a Set , access elements in a Set, add element/s to the Set, remove an element from the Set. and we will find out if we can update elements is a Set.

What is a set?

  • Set is unordered, and unindexed collection of data
  • Elements in a set are immutable, cannot be changed
  • Elements in a set must be unique

What is the use of a set?

  • Removing duplicates from a sequence
  • Computing standard mathematical operations like intersection, union, difference and symmetric difference.
  • Set has a highly optimized method to check if an element exists in the set

Creating a set

A set can be created by placing values inside {}.

In the example below, we have “Tokyo” and “Mumbai”as duplicate values but since set can contain only unique values we can see that the output contains “Tokyo” and “Mumbai” only once

we can also create a set using set() method. here you can see the elements are unordered

Accessing elements in a Set

since set is unordered collection of data, we cannot use index for access the elements in a set. We can use a for loop to access individual elements in a set

To check if an element is present in the set we can use the “in” operation.

In the below example, we check if Paris is present in the cities set, which returns False.

here we check if Tokyo is present in cities, which returns True

Adding elements to a set

To add a single element to a set we can use add() method.

If we want to add multiple elements to a set we need to use update()

Change existing elements in a set

we cannot change elements in a set as set is immutable

Removing elements is a set

To remove an element we can use remove() method. If the element does not exists then remove will raise a “KeyError”

we can also use discard() method to delete an element from the set. An error will not be raised if the element to be deleted does not exists in set.

pop() method randomly removes an element from the set.

we can use clear() method to remove all the elements from the set

Copying the elements from one set to another set

we can shallow copy elements from one set to another set. Shallow copy,when you change the content of one set and the other set remains unmodified.

Here we create cities and copy it’s content to copy_cities. we then add “Singapore” to the copt_cities set but see that cities set remains unmodified

Finding number of elements in a set

we use len() method to find the number of elements in a set

Set Operations

In this section we will explore Union, Intersection, difference and symmetric difference

we create two sets, one multiples of 2 and one multiples of 3

Union will takes all elements from the two sets, so we will have all unique values which are either multiples of 2 or 3 or both

Union is the grayed area between set_multiple_2 and set_multiple_3 with values 2,3,4,6,8,10,12,15

Intersection will take common elements between the two sets. In our example, we will have numbers that are both multiples of 2 and 3

Intersection is the grayed area with values 6 and 12 between set_multiple_2 and set_multiple_3

Difference is performed using difference() method or “-”.

Difference will return element only present in the set_muliple_2 set but not in the present in set_muliple_3.

In other words it will returns all elements of set_muliple_2 and remove the common elements between set_muliple_2 and set_muliple_3

Difference is the grayed area with values 2,4,8,10 between set_multiple_2 and set_multiple_3

Symmetric difference is performed using the “^” operator .

It will return unique elements between two sets but elements that are common between the two sets are excluded.

In our case, we will have all elements from the two sets and exclude the common elements between set_muliple_2 and set_muliple_3 which is 6 and 12

Symmetric Difference is the grayed area with values 2,3,4,8,9,10,15 between set_multiple_2 and set_multiple_3

Data Driven Investor

from confusion to clarity, not insanity

Renu Khandelwal

Written by

Loves learning, sharing and discovering myself. Retail SME and passionate about Machine Learning

Data Driven Investor

from confusion to clarity, not insanity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade