Adding and removing set elements in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we add or remove items from a set, we want to know how the time it takes changes as the set grows.
We ask: How does the work grow when the set gets bigger?
Analyze the time complexity of the following code snippet.
my_set = set()
for i in range(n):
my_set.add(i)
for i in range(n):
my_set.remove(i)
This code adds numbers from 0 to n-1 into a set, then removes them one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding and removing elements from the set.
- How many times: Each operation happens n times, once per loop iteration.
Each add or remove takes about the same time no matter the set size, so total work grows steadily with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (10 adds + 10 removes) |
| 100 | About 200 operations (100 adds + 100 removes) |
| 1000 | About 2000 operations (1000 adds + 1000 removes) |
Pattern observation: The total work grows in a straight line as n grows.
Time Complexity: O(n)
This means the time to add and remove all elements grows directly with the number of elements.
[X] Wrong: "Adding or removing from a set takes longer as the set gets bigger because it has to search through all elements."
[OK] Correct: Sets use a special way to find elements quickly, so each add or remove takes about the same time no matter the set size.
Understanding how set operations scale helps you explain why sets are great for fast lookups and changes, a useful skill in many coding problems.
"What if we used a list instead of a set for adding and removing elements? How would the time complexity change?"
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
