Bird
Raised Fist0
Pythonprogramming~20 mins

Set creation in Python - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
๐ŸŽ–๏ธ
Set Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of set creation with duplicates
What is the output of this Python code?
my_set = {1, 2, 2, 3, 4, 4, 4}
print(my_set)
Python
my_set = {1, 2, 2, 3, 4, 4, 4}
print(my_set)
A{1, 2, 3, 4}
B{1, 2, 2, 3, 4, 4, 4}
C[1, 2, 3, 4]
D(1, 2, 3, 4)
Attempts:
2 left
๐Ÿ’ก Hint
Remember that sets do not keep duplicates.
โ“ Predict Output
intermediate
2:00remaining
Creating a set from a list
What will be printed by this code?
my_list = [5, 6, 7, 5, 6]
my_set = set(my_list)
print(my_set)
Python
my_list = [5, 6, 7, 5, 6]
my_set = set(my_list)
print(my_set)
A(5, 6, 7)
B[5, 6, 7, 5, 6]
C{5, 6, 7}
D{5, 6, 7, 5, 6}
Attempts:
2 left
๐Ÿ’ก Hint
Converting a list to a set removes duplicates.
โ“ Predict Output
advanced
2:00remaining
Set comprehension with condition
What is the output of this code?
result = {x * x for x in range(5) if x % 2 == 0}
print(result)
Python
result = {x * x for x in range(5) if x % 2 == 0}
print(result)
A{0, 2, 4, 6, 8}
B{1, 9, 25}
C{0, 1, 4, 9, 16}
D{0, 4, 16}
Attempts:
2 left
๐Ÿ’ก Hint
Only even numbers from 0 to 4 are squared.
โ“ Predict Output
advanced
2:00remaining
Set creation with mutable elements
What error does this code raise?
my_set = {1, 2, [3, 4]}
Python
my_set = {1, 2, [3, 4]}
ATypeError
BSyntaxError
CValueError
DNo error, set created
Attempts:
2 left
๐Ÿ’ก Hint
Sets cannot contain mutable elements like lists.
๐Ÿง  Conceptual
expert
2:00remaining
Number of items in a set after complex creation
How many items are in the set after running this code?
data = [1, 2, 2, 3, 4, 4, 5]
result = {x for x in data if x % 2 != 0}
print(len(result))
Python
data = [1, 2, 2, 3, 4, 4, 5]
result = {x for x in data if x % 2 != 0}
print(len(result))
A7
B3
C4
D5
Attempts:
2 left
๐Ÿ’ก Hint
Count unique odd numbers in the list.

Practice

(1/5)
1. Which of the following is the correct way to create a set with the elements 1, 2, and 3 in Python?
easy
A. (1, 2, 3)
B. [1, 2, 3]
C. {1, 2, 3}
D. set[1, 2, 3]

Solution

  1. Step 1: Understand set creation syntax

    Sets are created using curly braces with comma-separated values or the set() function.
  2. Step 2: Identify correct syntax for set with elements

    {1, 2, 3} correctly creates a set with elements 1, 2, and 3.
  3. Final Answer:

    {1, 2, 3} -> Option C
  4. Quick Check:

    Curly braces with values = set [OK]
Hint: Use curly braces with commas for sets, not brackets [OK]
Common Mistakes:
  • Using square brackets creates a list, not a set
  • Using parentheses creates a tuple, not a set
  • Using set[] is invalid syntax
2. What is the correct way to create an empty set in Python?
easy
A. set()
B. {}
C. []
D. empty_set()

Solution

  1. Step 1: Recognize empty set syntax

    Using {} creates an empty dictionary, not a set.
  2. Step 2: Use the set() function for empty sets

    set() correctly creates an empty set.
  3. Final Answer:

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

    Empty set = set() function [OK]
Hint: Empty sets need set(), not curly braces [OK]
Common Mistakes:
  • Using {} creates an empty dictionary, not a set
  • Using [] creates an empty list
  • Using undefined functions like empty_set()
3. What will be the output of the following code?
my_set = {1, 2, 2, 3, 4, 4}
print(my_set)
medium
A. {1, 2, 2, 3, 4, 4}
B. [1, 2, 3, 4]
C. (1, 2, 3, 4)
D. {1, 2, 3, 4}

Solution

  1. Step 1: Understand set uniqueness property

    Sets automatically remove duplicate elements, so repeated values appear only once.
  2. Step 2: Apply uniqueness to given elements

    Duplicates 2 and 4 are removed, leaving {1, 2, 3, 4}.
  3. Final Answer:

    {1, 2, 3, 4} -> Option D
  4. Quick Check:

    Sets remove duplicates = {1, 2, 3, 4} [OK]
Hint: Sets keep unique items only, duplicates vanish [OK]
Common Mistakes:
  • Expecting duplicates to remain in the set
  • Confusing set output with list or tuple syntax
  • Thinking sets preserve order
4. Identify the error in the following code snippet:
my_set = set{1, 2, 3}
print(my_set)
medium
A. SyntaxError due to incorrect set creation syntax
B. Prints {1, 2, 3} correctly
C. TypeError because set() expects a list
D. NameError because set is not defined

Solution

  1. Step 1: Check set creation syntax

    The correct way to create a set using the function is set() with parentheses, not curly braces.
  2. Step 2: Identify syntax error

    set{1, 2, 3} is invalid syntax and causes a SyntaxError.
  3. Final Answer:

    SyntaxError due to incorrect set creation syntax -> Option A
  4. Quick Check:

    Use set() with parentheses, not braces [OK]
Hint: Use parentheses with set(), not curly braces [OK]
Common Mistakes:
  • Using curly braces after set instead of parentheses
  • Confusing set() with dictionary syntax
  • Assuming set is undefined
5. Given the list nums = [1, 2, 2, 3, 4, 4, 5], which code snippet correctly creates a set of unique elements from this list?
hard
A. unique_nums = {nums}
B. unique_nums = set(nums)
C. unique_nums = list(set(nums))
D. unique_nums = {1, 2, 3, 4, 5}

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    unique_nums = set(nums) -> Option B
  4. Quick Check:

    Use set() on list to get unique elements [OK]
Hint: Use set() on list to get unique items fast [OK]
Common Mistakes:
  • 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