Sets in Python

Sets in Python

Introduction

In math, a set is a group of things that are not necessarily in any specific order. With the additional constraints listed below, a Python set is identical to this mathematical definition.

A set's characteristics:

The built-in data structure in Python, known as a set, has the following three features.

1) Unordered: Unlike lists, the items in a set are not maintained in the order in which they are inserted. The items will be in a different sequence every time we access the Set object. Each item in the Set won't have an index value assigned to it.

2) Immutable: Set objects must be unchangeable. The set items cannot be altered, i.e., their values cannot be changed. But we have the option to change the Set's contents. Although the elements that make up a set may be changed, the Set itself must be immutable.

3) Unique: No two elements in the set can have the same value.

Set operations

Union, intersection, difference, complement, and other mathematical operations are frequently performed in Python using sets. As seen below, we can establish a set, get at its components, and do certain mathematical operations.

Assembling a group

Using the set() function or grouping all the components inside a pair of curly braces creates a set.

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Months={"Jan","Feb","Mar"}
Dates={21,22,17}
print(Days)
print(Months)
print(Dates)

Output:

{'Wed', 'Fri', 'Sun', 'Sat', 'Thu', 'Mon', 'Tue'}
{'Feb', 'Jan', 'Mar'}
{17, 21, 22}

When the code listed above is run, the outcome is as follows. Please take note of how the elements' relative order has altered.

write your code here: Coding Playground

Using a Set to Access Values

We are unable to access certain values within a set. All the components must be accessed at once as demonstrated above. But by iterating through the set in a loop, we can also obtain a list of distinct elements.

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])

for d in Days:
  print(d)

Output:

When the aforementioned code is run, the outcome is as follows:

Wed
Sun
Fri
Tue
Mon
Thu
Sat

write your code here: Coding Playground

Adding Items to a Set

The add() method can be used to add elements to a set. As was previously said, the newly inserted element is not associated with any particular index.

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])

Days.add("Sun")
print(Days)

Output :

The outcome of running the aforementioned code is as follows:

{'Wed', 'Fri', 'Mon', 'Tue', 'Thu', 'Sat', 'Sun'}

write your code here: Coding Playground

Removing Item from a Set

By utilizing the discard() method, we can eliminate elements from a set. As was previously said, the newly inserted element is not associated with any particular index.

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])

Days.discard("Sun")
print(Days)

Output:

The following outcome is produced when the aforementioned code is run.

{'Thu', 'Tue', 'Mon', 'Sat', 'Fri', 'Wed'}

write your code here: Coding Playground

Union of Sets

A new set comprising all the unique elements from both sets is created by performing a union operation on two sets. The element "Wed" in the example below is present in both sets.

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA|DaysB
print(AllDays)

Output

The following outcome is produced when the aforementioned code is run. Note that there is only one "wed" in the outcome.

{'Sun', 'Mon', 'Sat', 'Tue', 'Fri', 'Thu', 'Wed'}

write your code here: Coding Playground

Set intersection

When two sets are intersected, a new set is created that only contains the shared elements of the two sets. The element "Wed" in the example below is present in both sets.

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA & DaysB
print(AllDays)

Output

The following outcome is produced when the aforementioned code is run. Note that there is only one "wed" in the outcome.

{'Wed'}

Set Difference

When two sets are compared, the difference operation creates a new set that only contains elements from the first set and none from the second. The element "Wed" in the example below is present in both sets, hence the result set will not contain it.

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA - DaysB
print(AllDays)

Output

The following outcome is produced when the aforementioned code is run. Note that there is only one "wed" in the outcome.

{'Mon', 'Tue'}

Compare Sets

It is possible to determine whether a set is a subset or superset of another set. Depending on the elements in the sets, the outcome is either True or False.

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
SubsetRes = DaysA <= DaysB
SupersetRes = DaysB >= DaysA
print(SubsetRes)
print(SupersetRes)

Output

The outcome of running the aforementioned code is the following:

True
True