Bird
Raised Fist0
Pythonprogramming~10 mins

Adding and removing set elements in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Adding and removing set elements
Start with empty set
Add element with add()
Check if element exists
Remove element with remove() or discard()
Set updated
End
This flow shows starting with a set, adding elements, checking existence, removing elements, and ending with the updated set.
Execution Sample
Python
s = set()
s.add(10)
s.add(20)
s.remove(10)
print(s)
This code creates a set, adds two elements, removes one, and prints the final set.
Execution Table
StepActionSet ContentOutput/Note
1Create empty set sset()Set is empty
2Add 10 with s.add(10){10}10 added
3Add 20 with s.add(20){10, 20}20 added
4Remove 10 with s.remove(10){20}10 removed
5Print s{20}Outputs: {20}
💡 Program ends after printing the set with one element {20}
Variable Tracker
VariableStartAfter 1After 2After 3Final
sset(){10}{10, 20}{20}{20}
Key Moments - 2 Insights
What happens if we try to remove an element not in the set?
Using remove() on a missing element causes an error, but discard() does not. See step 4 where remove(10) works because 10 is present.
Does adding the same element twice change the set?
No, sets only keep unique elements. Adding 10 twice would not add a second 10. This is why after step 2, adding 10 again would not change the set.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the content of set s after step 3?
A{20}
B{10, 20}
C{10}
D{}
💡 Hint
Check the 'Set Content' column at step 3 in the execution_table.
At which step is the element 10 removed from the set?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column describing removal in the execution_table.
If we replace s.remove(10) with s.discard(30), what happens?
ANo error, set remains unchanged
BError occurs because 30 is not in the set
C30 is added to the set
DSet becomes empty
💡 Hint
Recall that discard() does not raise an error if the element is missing, unlike remove().
Concept Snapshot
Set operations:
- Create: s = set()
- Add element: s.add(x)
- Remove element: s.remove(x) (error if missing)
- Remove safely: s.discard(x) (no error if missing)
- Sets hold unique elements only
Full Transcript
This visual execution shows how to add and remove elements from a Python set. We start with an empty set, add elements 10 and 20, then remove 10. The set updates after each operation. Removing an element not in the set with remove() causes an error, but discard() avoids errors. Adding the same element twice does not duplicate it. The final printed set contains only 20.

Practice

(1/5)
1. Which method is used to add a new element to a Python set?
easy
A. add()
B. append()
C. insert()
D. push()

Solution

  1. Step 1: Understand set methods

    Sets in Python use add() to add elements, unlike lists which use append().
  2. Step 2: Identify correct method for sets

    insert() and push() are not valid set methods.
  3. Final Answer:

    add() -> Option A
  4. Quick Check:

    Use add() to add elements to sets [OK]
Hint: Remember: sets use add(), lists use append() [OK]
Common Mistakes:
  • Confusing list methods with set methods
  • Trying to use append() on sets
  • Using insert() which is for lists
2. Which of the following is the correct syntax to remove an element 5 from a set s without causing an error if 5 is not present?
easy
A. s.remove(5)
B. s.delete(5)
C. s.pop(5)
D. s.discard(5)

Solution

  1. Step 1: Understand remove() vs discard()

    remove() raises an error if the element is missing, but discard() does not.
  2. Step 2: Check method validity

    delete() is not a set method, and pop() removes an arbitrary element without arguments.
  3. Final Answer:

    s.discard(5) -> Option D
  4. Quick Check:

    Use discard() to safely remove elements [OK]
Hint: Use discard() to avoid errors when removing [OK]
Common Mistakes:
  • Using remove() without checking element presence
  • Trying to use delete() which doesn't exist
  • Passing arguments to pop() which takes none
3. What will be the output of the following code?
fruits = {'apple', 'banana', 'cherry'}
fruits.add('orange')
fruits.remove('banana')
print(fruits)
medium
A. {'apple', 'cherry', 'orange'}
B. {'apple', 'banana', 'cherry', 'orange'}
C. {'apple', 'banana', 'orange'}
D. Error

Solution

  1. Step 1: Add 'orange' to the set

    Using add('orange') adds 'orange' to the set, so now it has {'apple', 'banana', 'cherry', 'orange'}.
  2. Step 2: Remove 'banana' from the set

    remove('banana') deletes 'banana', leaving {'apple', 'cherry', 'orange'}.
  3. Final Answer:

    {'apple', 'cherry', 'orange'} -> Option A
  4. Quick Check:

    add() adds, remove() deletes existing element [OK]
Hint: Add then remove changes set contents accordingly [OK]
Common Mistakes:
  • Expecting banana to remain after remove()
  • Thinking add() replaces elements
  • Confusing set order in output
4. The following code throws an error. What is the cause and how to fix it?
numbers = {1, 2, 3}
numbers.remove(4)
print(numbers)
medium
A. SyntaxError due to wrong method; fix by using delete(4)
B. No error; output is {1, 2, 3}
C. KeyError because 4 is not in set; fix by using discard(4)
D. TypeError because remove() needs a list; fix by converting set to list

Solution

  1. Step 1: Identify error cause

    remove(4) raises a KeyError because 4 is not in the set.
  2. Step 2: Fix error using discard()

    Replacing remove(4) with discard(4) avoids error even if 4 is missing.
  3. Final Answer:

    KeyError because 4 is not in set; fix by using discard(4) -> Option C
  4. Quick Check:

    remove() errors if missing; discard() does not [OK]
Hint: Use discard() to avoid errors when unsure element exists [OK]
Common Mistakes:
  • Assuming remove() never errors
  • Trying to use delete() which is invalid
  • Confusing error types
5. Given a set 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?
hard
A. nums.add(6) nums.remove(2)
B. nums.add(6) nums.discard(2)
C. nums.append(6) nums.discard(2)
D. nums.add(6) nums.delete(2)

Solution

  1. Step 1: Add element 6 correctly

    add(6) is the correct method to add an element to a set.
  2. Step 2: Remove element 2 safely

    discard(2) removes 2 without error if missing; remove(2) could cause error.
  3. Final Answer:

    nums.add(6) nums.discard(2) -> Option B
  4. Quick Check:

    add() to add, discard() to safely remove [OK]
Hint: Add with add(), remove safely with discard() [OK]
Common Mistakes:
  • Using append() which is for lists
  • Using remove() without checking element presence
  • Trying to use delete() which doesn't exist