Sets let you store unique items. Adding and removing elements helps you change what is in the set easily.
Adding and removing set elements in Python
Start learning this pattern below
Jump into concepts and practice - no test required
my_set.add(element) my_set.remove(element) my_set.discard(element) my_set.pop()
add() adds an element to the set.
remove() deletes an element but causes an error if the element is not found.
discard() deletes an element but does not cause an error if the element is missing.
pop() removes and returns an arbitrary element from the set.
fruits = {'apple', 'banana'}
fruits.add('orange')fruits.remove('banana')fruits.discard('grape')item = fruits.pop()
This program shows how to add and remove elements from a set. It adds 'bird', removes 'cat', tries to discard 'fish' safely, and pops an element.
my_set = {'cat', 'dog'}
print('Original set:', my_set)
my_set.add('bird')
print('After adding bird:', my_set)
my_set.remove('cat')
print('After removing cat:', my_set)
my_set.discard('fish') # no error if not present
print('After discarding fish:', my_set)
popped = my_set.pop()
print('Popped element:', popped)
print('Set after pop:', my_set)Sets do not keep order, so elements may appear in any order when printed.
Use discard() if you want to avoid errors when removing elements that might not be in the set.
pop() removes a random element because sets are unordered.
Use add() to add elements to a set.
Use remove() to delete elements but be careful if the element might not exist.
Use discard() to delete elements safely without errors.
Practice
Solution
Step 1: Understand set methods
Sets in Python useadd()to add elements, unlike lists which useappend().Step 2: Identify correct method for sets
insert()andpush()are not valid set methods.Final Answer:
add() -> Option AQuick Check:
Use add() to add elements to sets [OK]
- Confusing list methods with set methods
- Trying to use append() on sets
- Using insert() which is for lists
5 from a set s without causing an error if 5 is not present?Solution
Step 1: Understand remove() vs discard()
remove()raises an error if the element is missing, butdiscard()does not.Step 2: Check method validity
delete()is not a set method, andpop()removes an arbitrary element without arguments.Final Answer:
s.discard(5) -> Option DQuick Check:
Use discard() to safely remove elements [OK]
- Using remove() without checking element presence
- Trying to use delete() which doesn't exist
- Passing arguments to pop() which takes none
fruits = {'apple', 'banana', 'cherry'}
fruits.add('orange')
fruits.remove('banana')
print(fruits)Solution
Step 1: Add 'orange' to the set
Usingadd('orange')adds 'orange' to the set, so now it has {'apple', 'banana', 'cherry', 'orange'}.Step 2: Remove 'banana' from the set
remove('banana')deletes 'banana', leaving {'apple', 'cherry', 'orange'}.Final Answer:
{'apple', 'cherry', 'orange'} -> Option AQuick Check:
add() adds, remove() deletes existing element [OK]
- Expecting banana to remain after remove()
- Thinking add() replaces elements
- Confusing set order in output
numbers = {1, 2, 3}
numbers.remove(4)
print(numbers)Solution
Step 1: Identify error cause
remove(4)raises a KeyError because 4 is not in the set.Step 2: Fix error using discard()
Replacingremove(4)withdiscard(4)avoids error even if 4 is missing.Final Answer:
KeyError because 4 is not in set; fix by using discard(4) -> Option CQuick Check:
remove() errors if missing; discard() does not [OK]
- Assuming remove() never errors
- Trying to use delete() which is invalid
- Confusing error types
nums = {1, 2, 3, 4, 5}, which code snippet correctly adds 6 and removes 2 safely without errors, even if 2 might not be present?Solution
Step 1: Add element 6 correctly
add(6)is the correct method to add an element to a set.Step 2: Remove element 2 safely
discard(2)removes 2 without error if missing;remove(2)could cause error.Final Answer:
nums.add(6) nums.discard(2) -> Option BQuick Check:
add() to add, discard() to safely remove [OK]
- Using append() which is for lists
- Using remove() without checking element presence
- Trying to use delete() which doesn't exist
