Set Methods in Python

Sets are quite useful as these are mutable, un-ordered collection of objects and only contain unique elements(No Duplicate Elements at all). That’s why there does exist many methods which can be applied to Sets as compared to other Sequence Types in Python.
Let’s straight forward get into what are these Set Methods in Python?

add() Set Method in Python

This Set Method – add() will add element to a set, if it already doesn’t have it. For example – If you have a set laptops = {“MAC”, “HP”, “Lenovo”} then to add “LG” to set laptops. You can do like laptops.add(“LG”). Here add() method will first look into set itself to see whether element is already there or not. As in this case its not already there. So add() method will add newer element to set laptops.
Below are some code examples of add() method for Sets in Python

# Example 1

fruits = {"grapes", "banana", "apple"}
fruits.add("pomegranate")
print(fruits)                  # Returns {"grapes", "banana", "apple", "pomegranate"}


# Example 2

cars = {"Tesla", "GM"}
cars.add("Volswagen")
print(cars)                    # Returns {"Tesla", "GM", "Volswagen"}

clear() Set Method in Python

This method can be used for completely emptying a set. If clear() method is called upon a set then it will clear up all elements in Set and returns an empty set. For example – If there is a set named set1 = {38, 29, 193} then doing set1.clear() will turn set1 into an empty set {} or set().
Below are some code example of clear() method for Sets in Python.

# Example 1

fruits = {"grapes", "banana", "apple"}
fruits.clear()
print(fruits)                  # Returns empty set

# Example 2

cars = {"Tesla", "GM"}
cars.clear()
print(cars)                    # Returns empty set

copy() Set Method in Python

copy() method in Python returns a copy of Set. For example – If you have set1 = {18, 29, 19} then doing set2 = set1.copy() will create a newer set2.
Note here that set2 and set1 points to different memory location, meaning changing set1 will not change set2 and vice-versa.

set1 = {18, 29, 19}
set2 = set1.copy()
print(set2)              # Returns {18, 29, 19}

# Here changing set1 doesn't change set2 and vice-versa

# Adding some value to set1
set1.add(10)
print(set1)              # Returns {18, 29, 19, 10}

print(set2)              # Returns {18, 29, 19}

# Although set1 have changed set2 still is same. Meaning that set1, set2 are stored at
# different memory locations.

But what if you need to make two copies of same set for some reason and you want other copy to change as you change one set. Like if you have set1 = {19, 20} and then as you change set1 you want some copy of this set to change simultaneously.
Below is an example showing how this can be done in Python.

# Code example showing copying of Sets in Python

set1 = {10, 29, 182, 19}
set2 = set1
print(set2)            # Returns {10, 29, 182, 19}

# Adding something to set1
set1.add(1000)
print(set1)            # Returns {1000, 10, 29, 182, 19}

print(set2)            # Returns {1000, 10, 29, 182, 19}

Here its clear that Sets created using equal operator(=) just points to same memory location in Python.

discard() Set Method in Python

discard() method just removes the specified element from set if its there in Set, but doesn’t raise any error if element is not in set rather just returns set itself in this case.
For example – If you have set1 = {19, 18, 45} then doing set1.discard(19) would remove 19 from set1 and now set1 will be {18, 45}.
Below are some code examples explaining discard() method in Python.

# Code example showing How to use discard() set method in Python

# Example1
set1 = {65, 29, 284}
set1.discard(29)
print(set1)           # Returns {65, 284}


# Example2
set3 = {"Hello", "How are you", "I am a programmer", "I loves life"}
set3.discard("Hello")
print(set3)           # Returns {"How are you", "I am a programmer", "I loves life"}

pop() Set Method in Python

pop() set method just removes an element from a set randomly. This method just randomly picks up element from set – remove it and also return removed element.
Below are some code examples explaining use of pop() Set Method in Python Programming Langauge.

# Code example showing use of pop() method in Python

set1 = (10, 99, 1982)
set1.pop()            # Returns anyone of three elements in set1

remove() Set method in Python

remove() method takes a single parameter and removes that element from set and doesn’t return anything.
Below are some code examples showing How to remove() set method in Python?

# Example

set1 = {29, 18, 891, 192}
set1.remove(29)         # Returns nothing but set1 has been modified
print(set1)             # Returns {18, 891, 192}

Some other Set Operations and Methods in Python

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

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

Recent Posts