Python sets | Python assembly operations


This post was made public as part of the Data Science Blogathon

Sets are an example of essential Python data structures.


Data structures are the building house of Python scripts. Maintain or include data carefully so that workers operate more efficiently. Therefore, it is of fundamental relevance to investigate how to cooperate with data structures.

A set is a combination of objects with the last characteristic points.

  • Sets are messy
  • Sets contain different items.
  • Items in a set require to be permanent, In other words, immutable.

How the sets are messy, we cannot do operations like indexing and segmenting sets. A set does not make it possible to have mutable objects as lists as an element in a Python set. A common use case for assemblies is to delete duplicate parts from a combination or sequence.

In this report, we will discover 5 commonly used operations on sets. Let's start by generating a set. We can practice the set constructor on separate collections to build a set.

mylst = ['A', 'A', 'B', 'A', 'C', 'B']
myst = set(mylst)
Output: {'A', 'B', 'C'}

We have built a set from a list. The set only covers the unique items in the list. The set constructor can also be implemented in a NumPy array. Here is the code for that:

import numpy as np
arr = np.random.randint(0, 5, size=20)
myst = set(arr)
Output: [4 0 4 3 1 1 3 0 0 1 3 4 0 3 2 4 1 4 3 3]
Output: {0, 1, 2, 3, 4}


1.Add and delete items

It is a simple method to join or delete items. Add and remove methods apply, respectively:

Output: {0, 1, 2, 3, 4, 5}

If we decide to add an element previously in the set, the set will stay the same and we will not get a notice or failure.

Output: {0, 1, 2, 3, 4, 5}

The application of the delete method is identical.

Output: {0, 1, 2, 3, 5}

2.Update a set

Updating one set with another set involves placing the items from the second set in the original set. Analyze the two resulting sets.

myst = set([0, 1, 2, 3, 5])
myotherst = set([3, 4, 5, 6, 7])

We can modernize “myst” by “myotherst” in the next way:

Output: {0, 1, 2, 3, 4, 5, 6, 7}

The update approach is beneficial because we don't need to worry about the equivalent and multiple items in both sets..

We can also renew a set with independent data structures, as lists or tuples.

myst = set([0, 1, 2, 3, 5])
mylst = [1, 2, 10,11,12]
Output: {0, 1, 2, 3, 5, 10, 11, 12}

3.Combining sets

The upgrade method operates on the spot, which implies that it alters the primary set. In some circumstances, we need a mix of several sets without renewing the originals. The joining system reflects the quality of two sets, so we can attach it to a different variable.

myst = {'A', 'B', 'C'}
newst = {'B', 'C', 'D', 'E'}
newst2 = {1, 2, 3}
combinedst = myst.union(newst).union(newst2)print(myst)
Output: {'A', 'B', 'C'}
Output: {'A', 1, 2, 'D', 'E', 3, 'B', 'C'}

We get a sequence (In other words, Union) of the sets, but the first sets remain the same.

In the above case, also we have noticed a strange way of constructing a set: parts can be added inside braces (“{}”) to the whole set.

4 set comparison

Two sets can be examined in terms of the items they contain. The issuperset and issubset methods can be used to relate two sets.

Suppose we have two sets called A and B. If A includes all the ingredients in B, then A is a superset of B. Then, B is a subset of A.

A = {1, 2, 3, 4, 5}
B = {1, 4, 5}
C = {1, 4, 6}
Output: True
Output: True
Output: False

One of the parts of set C is not in set A. Therefore, A is not a superset of C.

If two sets contain identical items, both the superset and the subset can be considered to each other.

D = {1, 4, 5}
E = {1, 4, 5}
Output: True
Output: True

5.The intersection and the difference

Set theory is quite comparable to Venn graphs in mathematics (statistics).

We could be involved in the items that are in a set, but not the opposite. At the same time, we may need to discover the highlights found in both sets. Diversity and intersection methods can be used to implement these actions, respectively.

A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7, 8}
Output: {1, 2}
Output: {3, 4, 5}
Output: {6, 7, 8}

The distribution of the sets is not expressed when determining the intersection. Despite this, the deviation is determined based on the order. The diversity of A from B includes items in A but not in B and vice versa.


We have made cases to illustrate 5 daily operations done in sets. More methods can be used for sets. Despite this, what we have incorporated in this post is suitable for most maximum cases.

Connect with me on my social networks: HALFLINKEDINGITHUB. Thanks for browsing.

The media shown in this post is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.