- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Questions & Answers
- Python - Online Quiz
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - Remove Set Items
Remove Set Items
Removing set items implies deleting elements from a set. In Python, sets are mutable, unordered collections of unique elements, and there are several methods available to remove items from a set based on different criteria.
We can remove set items in Python using various methods such as remove(), discard(), pop(), clear(), and set comprehension. Each method provide different ways to eliminate elements from a set based on specific criteria or conditions.
Remove Set Item Using remove() Method
The remove() method in Python is used to remove the first occurrence of a specified item from a set.
We can remove set items using the remove() method by specifying the element we want to remove from the set. If the element is present in the set, it will be removed. However, if the element is not found, the remove() method will raise a KeyError exception.
Example
In the following example, we are deleting the element "Physics" from the set "my_set" using the remove() method −
my_set = {"Rohan", "Physics", 21, 69.75} print ("Original set: ", my_set) my_set.remove("Physics") print ("Set after removing: ", my_set)
It will produce the following output −
Original set: {21, 69.75, 'Rohan', 'Physics'} Set after removing: {21, 69.75, 'Rohan'}
Example
If the element to delete is not found in the set, the remove() method will raise a KeyError exception −
my_set = {"Rohan", "Physics", 21, 69.75} print ("Original set: ", my_set) my_set.remove("PHP") print ("Set after removing: ", my_set)
We get the error as shown below −
Original set: {'Physics', 21, 69.75, 'Rohan'} Traceback (most recent call last): File "/home/cg/root/664c365ac1c3c/main.py", line 4, in <module> my_set.remove("PHP") KeyError: 'PHP'
Remove Set Item Using discard() Method
The discard() method in set class is similar to remove() method. The only difference is, it doesn't raise error even if the object to be removed is not already present in the set collection.
Example
In this example, we are using the discard() method to delete an element from a set regardless of whether it is present or not −
my_set = {"Rohan", "Physics", 21, 69.75} print ("Original set: ", my_set) # removing an existing element my_set.discard("Physics") print ("Set after removing Physics: ", my_set) # removing non-existing element my_set.discard("PHP") print ("Set after removing non-existent element PHP: ", my_set)
Following is the output of the above code −
Original set: {21, 'Rohan', 69.75, 'Physics'} Set after removing Physics: {21, 'Rohan', 69.75} Set after removing non-existent element PHP: {21, 'Rohan', 69.75}
Remove Set Item Using pop() Method
We can also remove set items using the pop() method by removing and returning an arbitrary element from the set. If the set is empty, the pop() method will raise a KeyError exception.
Example
In the example below, we are defining a set with elements "1" through "5" and removing an arbitrary element from it using the pop() method −
# Defining a set my_set = {1, 2, 3, 4, 5} # removing and returning an arbitrary element from the set removed_element = my_set.pop() # Printing the removed element and the updated set print("Removed Element:", removed_element) print("Updated Set:", my_set)
We get the output as shown below −
Removed Element: 1 Updated Set: {2, 3, 4, 5}
Example
If we try to remove element from an empty set, the pop() method will raise a KeyError exception −
# Defining an empty set empty_set = set() # Removing an arbitrary element from the empty set removed_element = empty_set.pop()
The error produced is as shown below −
Traceback (most recent call last): File "/home/cg/root/664c69620cd40/main.py", line 5, in <module> removed_element = empty_set.pop() KeyError: 'pop from an empty set'
Remove Set Item Using clear() Method
The clear() method in set class removes all the items in a set object, leaving an empty set.
We can remove set items using the clear() method by removing all elements from the set, effectively making it empty.
Example
In the following example, we are defining a set with elements "1" through "5" and then using the clear() method to remove all elements from the set −
# Defining a set with multiple elements my_set = {1, 2, 3, 4, 5} # Removing all elements from the set my_set.clear() # Printing the updated set print("Updated Set:", my_set)
It will produce the following output −
Updated Set: set()
Remove Items Existing in Both Sets
You can remove items that exist in both sets (i.e., the intersection of two sets) using the difference_update() method or the subtraction operator (-=). This removes all elements that are present in both sets from the original set.
Example
In this example, we are defining two sets "s1" and "s2", and then using the difference_update() method to remove elements from "s1" that are also in "s2" −
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} print ("s1 before running difference_update: ", s1) s1.difference_update(s2) print ("s1 after running difference_update: ", s1)
After executing the above code, we get the following output −
s1 before running difference_update: {1, 2, 3, 4, 5} s1 after running difference_update: {1, 2, 3} set()
Remove Items Existing in Either of the Sets
To remove items that exist in either of two sets, you can use the symmetric difference operation. The symmetric difference between two sets results in a set containing elements that are in either of the sets but not in their intersection.
In Python, the symmetric difference operation can be performed using the ^ operator or symmetric_difference() method.
Example
In the following example, we are defining two sets "set1" and "set2". We are then using the symmetric difference operator (^) to create a new set "result_set" containing elements that are in either "set1" or "set2" but not in both −
set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Removing items that exist in either set result_set = set1 ^ set2 print("Resulting Set:", result_set)
Following is the output of the above code −
Resulting Set: {1, 2, 5, 6}
Remove Uncommon Set Items
You can remove uncommon items between two sets using the intersection_update() method. The intersection of two sets results in a set containing only the elements that are present in both sets.
To keep only the common elements in one of the original sets and remove the uncommon ones, you can update the set with its intersection.
Example
In this example, we are defining two sets "set1" and "set2". We are then using the intersection_update() method to modify "set1" so that it only contains elements that are also in "set2" −
set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # Keeping only common items in set1 set1.intersection_update(set2) print("Set 1 after keeping only common items:", set1)
Output of the above code is as shown below −
Set 1 after keeping only common items: {3, 4}
The intersection() Method
The intersection() method in set class is similar to its intersection_update() method, except that it returns a new set object that consists of items common to existing sets.
Syntax
Following is the basic syntax of the intersection() method −
set.intersection(obj)
Where, obj is a set object.
Return value
The intersection() method returns a set object, retaining only those items common in itself and obj.
Example
In the following example, we are defining two sets "s1" and "s2", then using the intersection() method to create a new set "s3" containing elements that are common to both "s1" and "s2" −
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} print ("s1: ", s1, "s2: ", s2) s3 = s1.intersection(s2) print ("s3 = s1 & s2: ", s3)
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s3 = s1 & s2: {4, 5}
Symmetric Difference Update of Set Items
The symmetric difference between two sets is the collection of all the uncommon items, rejecting the common elements. The symmetric_difference_update() method updates a set with symmetric difference between itself and the set given as argument.
Example
In the example below, we are defining two sets "s1" and "s2", then using the symmetric_difference_update() method to modify "s1" so that it contains elements that are in either "s1" or "s2", but not in both −
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} print ("s1: ", s1, "s2: ", s2) s1.symmetric_difference_update(s2) print ("s1 after running symmetric difference ", s1)
The result obtained is as shown below −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s1 after running symmetric difference {1, 2, 3, 6, 7, 8}
Symmetric Difference of Set Items
The symmetric_difference() method in set class is similar to symmetric_difference_update() method, except that it returns a new set object that holds all the items from two sets minus the common items.
Example
In the following example, we are defining two sets "s1" and "s2". We are then using the symmetric_difference() method to create a new set "s3" containing elements that are in either "s1" or "s2", but not in both −
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} print ("s1: ", s1, "s2: ", s2) s3 = s1.symmetric_difference(s2) print ("s1 = s1^s2 ", s3)
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s1 = s1^s2 {1, 2, 3, 6, 7, 8}