Set creation in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we create a set from a list, we want to know how the time it takes changes as the list gets bigger.
We ask: How does the work grow when the input size grows?
Analyze the time complexity of the following code snippet.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
unique_numbers = set(numbers)
This code takes a list of numbers and creates a set to keep only unique values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each item from the list into the set.
- How many times: Once for each item in the list.
As the list gets bigger, the time to create the set grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 insertions |
| 100 | About 100 insertions |
| 1000 | About 1000 insertions |
Pattern observation: Doubling the input roughly doubles the work.
Time Complexity: O(n)
This means the time to create a set grows linearly with the number of items.
[X] Wrong: "Creating a set takes the same time no matter how many items there are."
[OK] Correct: Each item must be checked and added, so more items mean more work.
Understanding how set creation scales helps you explain efficiency clearly and shows you know how data structures behave with growing data.
"What if we created a set from a list that already has only unique items? How would the time complexity change?"
Practice
Solution
Step 1: Understand set creation syntax
Sets are created using curly braces with comma-separated values or the set() function.Step 2: Identify correct syntax for set with elements
{1, 2, 3}correctly creates a set with elements 1, 2, and 3.Final Answer:
{1, 2, 3} -> Option CQuick Check:
Curly braces with values = set [OK]
- Using square brackets creates a list, not a set
- Using parentheses creates a tuple, not a set
- Using set[] is invalid syntax
Solution
Step 1: Recognize empty set syntax
Using{}creates an empty dictionary, not a set.Step 2: Use the set() function for empty sets
set()correctly creates an empty set.Final Answer:
set() -> Option AQuick Check:
Empty set = set() function [OK]
- Using {} creates an empty dictionary, not a set
- Using [] creates an empty list
- Using undefined functions like empty_set()
my_set = {1, 2, 2, 3, 4, 4}
print(my_set)Solution
Step 1: Understand set uniqueness property
Sets automatically remove duplicate elements, so repeated values appear only once.Step 2: Apply uniqueness to given elements
Duplicates 2 and 4 are removed, leaving {1, 2, 3, 4}.Final Answer:
{1, 2, 3, 4} -> Option DQuick Check:
Sets remove duplicates = {1, 2, 3, 4} [OK]
- Expecting duplicates to remain in the set
- Confusing set output with list or tuple syntax
- Thinking sets preserve order
my_set = set{1, 2, 3}
print(my_set)Solution
Step 1: Check set creation syntax
The correct way to create a set using the function isset()with parentheses, not curly braces.Step 2: Identify syntax error
set{1, 2, 3}is invalid syntax and causes a SyntaxError.Final Answer:
SyntaxError due to incorrect set creation syntax -> Option AQuick Check:
Use set() with parentheses, not braces [OK]
- Using curly braces after set instead of parentheses
- Confusing set() with dictionary syntax
- Assuming set is undefined
nums = [1, 2, 2, 3, 4, 4, 5], which code snippet correctly creates a set of unique elements from this list?Solution
Step 1: Understand how to convert list to set
The set() function can take an iterable like a list and return a set of unique elements.Step 2: Analyze each option
unique_nums = {nums}creates a set with the entire list as one element (invalid).unique_nums = set(nums)correctly converts the list to a set.unique_nums = list(set(nums))converts to set then back to list, which is not a set.unique_nums = {1, 2, 3, 4, 5}manually writes the set but is not dynamic.Final Answer:
unique_nums = set(nums) -> Option BQuick Check:
Use set() on list to get unique elements [OK]
- Trying to put list inside curly braces directly
- Converting set back to list when set is needed
- Hardcoding values instead of using the list variable
