
- 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
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Object & Classes
- 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 - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - Remove Set Items
Python's set class provides different methods to remove one or more items from a set object.
Remove Set Items
The remove() method removes the given item from the set collection, if it is present in it. However, if it is not present, it raises KeyError.
Syntax
set.remove(obj)
Parameters
obj − an immutable object
Example
lang1 = {"C", "C++", "Java", "Python"} print ("Set before removing: ", lang1) lang1.remove("Java") print ("Set after removing: ", lang1) lang1.remove("PHP")
It will produce the following output −
Set before removing: {'C', 'C++', 'Python', 'Java'} Set after removing: {'C', 'C++', 'Python'} lang1.remove("PHP") KeyError: 'PHP'
Remove Set Items Ignoring Error
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.
Syntax
set.discard(obj)
Parameters
obj − An immutable object
Example
lang1 = {"C", "C++", "Java", "Python"} print ("Set before discarding C++: ", lang1) lang1.discard("C++") print ("Set after discarding C++: ", lang1) print ("Set before discarding PHP: ", lang1) lang1.discard("PHP") print ("Set after discarding PHP: ", lang1)
It will produce the following output −
Set before discarding C++: {'Java', 'C++', 'Python', 'C'} Set after discarding C++: {'Java', 'Python', 'C'} Set before discarding PHP: {'Java', 'Python', 'C'} Set after discarding PHP: {'Java', 'Python', 'C'}
Remove Random Item from Set
The pop() method in set class removes an arbitrary item from the set collection. The removed item is returned by the method. Popping from an empty set results in KeyError.
Syntax
obj = set.pop()
Return value
The pop() method returns the object removed from set.
Example
lang1 = {"C", "C++"} print ("Set before popping: ", lang1) obj = lang1.pop() print ("object popped: ", obj) print ("Set after popping: ", lang1) obj = lang1.pop() obj = lang1.pop()
It will produce the following output −
Set before popping: {'C++', 'C'} object popped: C++ Set after popping: {'C'} Traceback (most recent call last): obj = lang1.pop() ^^^^^^^^^^^ KeyError: 'pop from an empty set'
At the time of call to pop() for third time, the set is empty, hence KeyError is raised.
Remove All Set Items
The clear() method in set class removes all the items in a set object, leaving an empty set.
Syntax
set.clear()
Example
lang1 = {"C", "C++", "Java", "Python"} print (lang1) print ("After clear() method") lang1.clear() print (lang1)
It will produce the following output −
{'Java', 'C++', 'Python', 'C'} After clear() method set()
Remove Items that Exist in Both Sets
The difference_update() method in set class updates the set by removing items that are common between itself and another set given as argument.
Syntax
set.difference_update(obj)
Parameters
obj − a set object
Example
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)
It will produce the following output −
s1 before running difference_update: {1, 2, 3, 4, 5} s1 after running difference_update: {1, 2, 3} set()
Remove Items that Exist in Either Set
The difference() method is similar to difference_update() method, except that it returns a new set object that contains the difference of the two existing sets.
Syntax
set.difference(obj)
Parameters
obj − a set object
Return value
The difference() method returns a new set with items remaining after removing those in obj.
Example
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} print ("s1: ", s1, "s2: ", s2) s3 = s1.difference(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: {1, 2, 3}
Remove Uncommon Set Items
As a result of intersection_update() method, the set object retains only those items which are common in itself and other set object given as argument.
Syntax
set.intersection_update(obj)
Parameters
obj − a set object
Return value
The intersection_update() method removes uncommon items and keeps only those items which are common to itself and obj.
Example
s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} print ("s1: ", s1, "s2: ", s2) s1.intersection_update(s2) print ("a1 after intersection: ", s1)
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s1 after intersection: {4, 5}
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
set.intersection(obj)
Parameters
obj − a set object
Return value
The intersection() method returns a set object, retaining only those items common in itself and obj.
Example
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.
Syntax
set.symmetric_difference_update(obj)
Parameters
obj − a set object
Example
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)
It will produce the following output −
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.
Syntax
set.symmetric_difference(obj)
Parameters
obj − a set object
Return value
The symmetric_difference() method returns a new set that contains only those items not common between the two set objects.
Example
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}
To Continue Learning Please Login