It is a method in python set that removes the specified element from the current set. If it doesn’t exist then it throws a KeyError Exception. It doesn’t return any value and modifies the given set.
Syntax:
set.remove(element)
Also Read: Python Set – add() method
Example 1:
# Python code to understand remove() method in set
# www.codewindow.in
s = {"amazing", "code", "best"}
s.remove("code")
print(s)
Output:
{'amazing', 'best'}
Explanation:
Here the element “code” has been removed from the parent set (s).
Example 2:
# Python code to understand remove() method in set
# www.codewindow.in
s = {5, 8, 4, 1, 5, 9}
s.remove(5)
print(s)
Output:
{1, 4, 8, 9}
Explanation:
Here the element “5” has been removed from the parent set (s).
Note: set doesnt allow duplicate element.
Example 3:
# Python code to understand remove() method in set
# www.codewindow.in
s = {5, 8, 4, 1, 5, 9}
s.remove(6)
print(s)
Output:
Traceback (most recent call last):
File "./prog.py", line 2, in
KeyError: 6
Explanation:
Here it throws an exception since the element is not present on the set. For this we use the method discard() which doesn’t throws an exception.
Follow Us
You Missed
- Dunzo Off-Campusing Drive | Data Engineer | codewindow.in
- Verbal Ability Questions Solved | Synonyms Antonyms – codewindow.in
- Verbal Ability Questions Solved | Statement Correction – codewindow.in
- Verbal Ability Questions Solved | Spotting Error – codewindow.in
- Verbal Ability Questions Solved | Speech and Tenses – codewindow.in
- Try
- Verbal Ability Questions Solved | Sentence Arrangement – codewindow.in
- Verbal Ability Questions Solved | Prepositions – codewindow.in
- Verbal Ability Questions Solved | Grammar – codewindow.in
- Verbal Ability Questions Solved | Articles – codewindow.in
- Zoho Off Campus Drive | Product Marketing Associate / Specialist | codewindow.in
- BerryWorks Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- Microsoft Off Campus Hiring Drive – Technical Support – Codewindow.in
- Infosys Pseudocede & Puzzle Solved – codewindow.in
- Infosys Verbal Questions Solved – codewindow.in
- Amazon Off Campus Drive | Trainee | codewindow.in
- Hawkins Off Campus Drive | Management trainees | codewindow.in
- Infosys Mathematical Questions Solved – codewindow.in
- Paypal Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- IBM Off Campus Hiring Drive – Associate Systems Engineer – Codewindow.in
- Infosys Off Campus Hiring Drive – SP and DSE – Codewindow.in
- VIRTUSA Recruitment 2022 | Associate Engineer | codewindow.in
- Infosys Logical Questions solved – codewindow.in
- Google Off Campus Hiring Drive – IT Support Engineer – Codewindow.in
- Capgemini Pseudocode Solved | Set 4 – codewindow.in
- Capgemini Pseudocode Solved | Set 3 – codewindow.in
- Capgemini Pseudocode Solved | Set 2 – codewindow.in
- JECA 2022 Question Paper – 90+ Sample Questions Exposed – codewindow.in
- Capgemini Pseudocode Solved | Set 1 – codewindow.in
- Verizon Off Campusing | Software Developer | codewindow.in
- TCS Recruitment Off-Campusing | Service Desk Analyst | codwindow.in
- Amdocs Off-Campusing | Devops Engineer | codewindow.in
Also Checkout
- Algorithm
- Aptitude
- Capgemini Coding Questions
- Capgemini Pseudocode
- CodeVita
- Coding Questions
- Cognizant Placement
- Data Structure and Algorithm
- Epam Full Question Paper
- Guidance for Accenture
- HR Questions
- IBM Questions
- Infosys
- Internship
- Interview Experience
- JECA
- Job Info
- Machine Learning
- Miscellaneous
- NPCI
- Programming in C
- Programming in C++
- Programming in JAVA
- Programming in Python
- Quiz
- Recruiting Companies
- Revature
- Study Material
- TCS NQT
- Tech Mahindra Coding Questions
- Tech Mahindra Questions
- Uncategorized
- Verbal Ability
- Web Development
- Wipro Coding Questions
- Wipro NLTH
- WIpro NLTH Coding Solve