In this article Concept of Sets in Python we give the information about Sets are a type of data structure used in Python. These are unordered and unindexed. Each item in a set is unique and despite being mutable, the items added to it cannot themselves be changed.

Concept of Sets in Python:

Sets are a type of data structure used in Python. These are unordered and unindexed. Each item in a set is unique and despite being mutable, the items added to it cannot themselves be changed. Sets are mainly used to remove duplicates and perform mathematical operations such as union, intersection, and difference.

Defining set

Sets in Python are created using {} (curly braces) or the set() function.

# Example 1:

Creating a Set

my_set = {1, 2, 3, 4, 5}

print(my_set)

# Example 2:

using set()

another_set = set([1, 2, 2, 3, 4])

print(another_set)

Output:

{1, 2, 3, 4, 5}

{1, 2, 3, 4}

# Duplicates removed

Set Features

1. Unordered: There is no fixed order of items in the set.

2. No Duplicates:  There cannot be any duplicate items in the set.

3. Mutable: You can add or remove new items from the set.

Initializing and accessing a set

Initializing a Set1.

Using curly braces {}:

# Example: Using curly braces

my_set = {1, 2, 3, 4}

print(my_set)

Output:{1, 2, 3, 4}

2. By using set() function:

# Example: Using set() function

empty_set = set()

# empty set

print(empty_set)

another_set = set([1, 2, 2, 3])

# Creating set from list

print(another_set)

Output:

set()

{1, 2, 3}

Note: {} does not work to create an empty set, it creates a dictionary. Always use set() to create an empty set.

Accessing Elements in a Set:

The set is unordered, so no item in it can be accessed by index. But you can access the items in the following way:

1. Accessing Items through Loop:

# Example:

Using Loop

my_set = {10, 20, 30, 40} for item in my_set:print(item)

Output:10  20  30  40

2. Checking using in operator:

# Example: Checking the presence of an item

my_set = {1, 2, 3, 4}

print(3 in my_set) # True

print(5 in my_set) # False

Output:

True  False

Adding and Removing Elements

1. Adding items:

my_set = {1, 2, 3}

my_set.add(4)

#add 4

print(my_set)

Output:{1, 2, 3, 4}

2. Adding more than one item (update):

my_set = {1, 2}

my_set.update([3, 4, 5])

# Add item from list

print(my_set)

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

3. Remove and discard items:

my_set = {1, 2, 3, 4}

my_set.remove(3) remove #3

print(my_set)

my_set.discard(2)

#remove 2

print(my_set)

Output:

{1, 2, 4}

{1, 4}

Note: remove() will return an error if the item does not exist, whereas discard() will not.

4. Clearing all items:

my_set = {1, 2, 3}

my_set.clear()

# clear the set

print(my_set)

Output:

set().

Conclusion:

Sets can be initialized using {} or set().

It is not possible to access items by index.

Items can be accessed using for loops and in operators.

add(), update(), remove(), and discard() are used to add and remove items from the set.

Sets Operation in python:

1. Union

Union means combining two or more sets and creating a set that contains all the unique items.

Syntax:

set1.union(set2)

# Orset1 | set2

Example:

A = {1, 2, 3}

B = {3, 4, 5}

#union operation

print(A.union(B))

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

print(A | B)

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

2. Intersection.

Intersection means finding items that are common across all sets.

Syntax:

set1.intersection(set2)

# Orset1 & set2

Example:

A = {1, 2, 3}

B = {3, 4, 5}

# intersection operation

print(A.intersection(B))# {3}

print(A & B)# {3}

3. Difference

Difference means removing those items from the first set that are not present in the second set.

Syntax:

set1.difference(set2)

# Orset1 – set2

Example:

A = {1, 2, 3}

B = {3, 4, 5}

# difference operation

print(A.difference(B))

# {1, 2}

print(A – B)

# {1, 2}

4. Symmetric Difference

Symmetric difference means removing items that are unique in both sets.

Syntax:

set1.symmetric_difference(set2)

# Or

set1^set2

Example:

A = {1, 2, 3}

B = {3, 4, 5}

# symmetric difference operation

print(A.symmetric_difference(B)) # {1, 2, 4, 5}

print(A ^ B)

# {1, 2, 4, 5}

5. Superset and Subset

Subset (issubset):

If a set is part of another set.

Superset (issuperset): If one set completely covers the other set.

Syntax:

set1.issubset(set2)

# check subsetset1.issuperset(set2)

# check superset

Example:

A = {1, 2}

B = {1, 2, 3}

print(A.issubset(B))

# True (A is a subset of B)

print(B.issuperset(A))

# True (B is a superset of A)

6. Adding and Removing Items

Adding items (add() and update()):

• add(): Adds an item.

• update(): Adds multiple items.

my_set = {1, 2}

my_set.add(3)

# add an item

my_set.update([4, 5])

# Add multiple items

print(my_set)

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

Removing items (remove() and discard()):

remove(): Removes the item. Returns an error if the item does not exist.

discard(): Removes the item, but does not return an error.

my_set = {1, 2, 3}

my_set.remove(2)

# remove 2

my_set.discard(4)

# remove 4 (not error if not present)

print(my_set)

# {1, 3}

7. Clearing and copying a set:

clear()

my_set = {1, 2, 3}

my_set.clear()

# clear the setprint(my_set)

# set()

copy()

my_set = {1, 2, 3}

new_set = my_set.copy()

# Make a copy of the setprint(new_set)

# {1, 2, 3}

8. Use of in operator (subscription check):

my_set = {1, 2, 3}

print(2 in my_set)

# True (2 exists)

print(5 in my_set)

# False (5 does not exist)

Conclusion:

Using sets operations in Python we can solve various mathematical and programming needs. Operations like union, intersection, and difference allow powerful use of sets.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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