0
0
Pythonprogramming~15 mins

Set creation in Python - Deep Dive

Choose your learning style9 modes available
Overview - Set creation
What is it?
A set in Python is a collection of unique items. Set creation means making a new set with some elements inside. Sets are unordered, so the items have no fixed position. They help you store things without duplicates.
Why it matters
Sets solve the problem of keeping only unique items easily. Without sets, you would have to write extra code to check for duplicates. This makes your programs simpler and faster when working with groups of things where repeats don't matter.
Where it fits
Before learning sets, you should know about lists and basic Python data types. After sets, you can learn about set operations like union and intersection, or explore dictionaries and other collections.
Mental Model
Core Idea
A set is like a bag that holds only unique things, ignoring order and duplicates.
Think of it like...
Imagine a basket where you put fruits, but if you try to add the same fruit twice, it only keeps one. You don't care about the order of fruits, just that each type is there once.
Set Creation Process:

  +-------------------+
  |  Elements to add  |
  +-------------------+
            |
            v
  +-------------------+
  |   Create a set    |
  |  (unique items)   |
  +-------------------+
            |
            v
  +-------------------+
  |   Resulting set   |
  |  {unique items}   |
  +-------------------+
Build-Up - 6 Steps
1
FoundationCreating an empty set
๐Ÿค”
Concept: How to make a set with no items inside.
In Python, you create an empty set using set() function. Using {} creates an empty dictionary, not a set. Example: empty_set = set() print(empty_set) print(type(empty_set))
Result
Output: set()
Understanding that {} is NOT an empty set but an empty dictionary prevents a common beginner mistake.
2
FoundationCreating a set with items
๐Ÿค”
Concept: How to create a set with some initial elements.
You can create a set by placing items inside curly braces {} separated by commas. Example: fruits = {'apple', 'banana', 'orange'} print(fruits)
Result
Output: {'apple', 'banana', 'orange'} (order may vary)
Knowing that sets use curly braces like dictionaries but only hold unique items helps you read and write set literals correctly.
3
IntermediateCreating sets from other collections
๐Ÿค”Before reading on: do you think you can create a set from a list or string directly? Commit to your answer.
Concept: Using the set() function to convert other collections into sets.
You can pass any iterable like a list, tuple, or string to set() to create a set of unique elements. Example: nums = [1, 2, 2, 3] unique_nums = set(nums) print(unique_nums) letters = set('hello') print(letters)
Result
Output: {1, 2, 3} {'h', 'e', 'l', 'o'} (order may vary)
Understanding that set() removes duplicates from any iterable is key to using sets for uniqueness.
4
IntermediateSets ignore duplicates automatically
๐Ÿค”Before reading on: if you add duplicate items in a set literal, will they appear multiple times? Commit to your answer.
Concept: Sets automatically remove duplicate items when created.
When you create a set with repeated items, Python keeps only one copy. Example: colors = {'red', 'blue', 'red', 'green', 'blue'} print(colors)
Result
Output: {'red', 'blue', 'green'} (order may vary)
Knowing that sets enforce uniqueness without extra code simplifies data cleaning and validation.
5
AdvancedImmutable elements in sets
๐Ÿค”Before reading on: can you put a list inside a set? Commit to your answer.
Concept: Sets can only contain immutable (unchangeable) elements like numbers, strings, or tuples.
Trying to add mutable items like lists or dictionaries to a set causes an error. Example: my_set = {1, 2, (3, 4)} # works my_set.add([5, 6]) # error Output: TypeError: unhashable type: 'list'
Result
Output: TypeError when adding mutable elements
Understanding immutability requirements prevents runtime errors and clarifies what sets can store.
6
ExpertSet creation performance and hashing
๐Ÿค”Before reading on: do you think sets store items in order or use a special method to find them quickly? Commit to your answer.
Concept: Sets use hashing to store and find items quickly, which affects how they are created and accessed.
When you create a set, Python computes a hash for each item to place it in a special structure for fast lookup. This is why items must be immutable and why sets have no order. This hashing process happens during set creation and affects performance. Example: import time large_list = list(range(1000000)) + list(range(500000)) start = time.time() large_set = set(large_list) end = time.time() print(f"Set creation took {end - start:.4f} seconds")
Result
Output: Set creation took (some small number) seconds
Knowing that sets rely on hashing explains their speed and limitations, helping you choose the right data structure.
Under the Hood
Python sets are implemented as hash tables. When you add an item, Python calculates its hash value, which is a number representing the item. This hash determines where the item is stored internally. Because of this, sets can quickly check if an item exists without scanning all elements. Items must be immutable so their hash doesn't change, ensuring consistent storage and retrieval.
Why designed this way?
Sets were designed to provide fast membership tests and uniqueness enforcement. Hash tables offer average constant time complexity for these operations. Alternatives like lists would be slower for large data. The immutability requirement ensures hash stability, preventing errors and data corruption.
Set Internal Structure:

+-------------------+
|   Input Element   |
+-------------------+
          |
          v
+-------------------+
|  Compute Hash     |
+-------------------+
          |
          v
+-------------------+
|  Hash Table Slot  |<---+  (array index based on hash)
+-------------------+    |
          |              |
          v              |
+-------------------+    |
| Store Element     |----+
+-------------------+
Myth Busters - 4 Common Misconceptions
Quick: Does {} create an empty set or an empty dictionary? Commit to your answer.
Common Belief:{} creates an empty set.
Tap to reveal reality
Reality:{} actually creates an empty dictionary. To create an empty set, you must use set().
Why it matters:Using {} for an empty set leads to bugs because you get a dictionary instead, which behaves differently.
Quick: Can sets contain lists as elements? Commit to your answer.
Common Belief:Sets can contain any type of element, including lists.
Tap to reveal reality
Reality:Sets cannot contain mutable elements like lists because they are unhashable and cause errors.
Why it matters:Trying to add mutable elements causes runtime errors, breaking your program unexpectedly.
Quick: Are sets ordered collections? Commit to your answer.
Common Belief:Sets keep the order of elements as they were added.
Tap to reveal reality
Reality:Sets are unordered; the order of elements is not preserved or guaranteed.
Why it matters:Relying on order in sets causes bugs and unpredictable behavior in your programs.
Quick: Does creating a set from a list keep duplicates? Commit to your answer.
Common Belief:Creating a set from a list keeps all duplicates.
Tap to reveal reality
Reality:Creating a set removes duplicates automatically, keeping only unique items.
Why it matters:Misunderstanding this leads to wrong assumptions about data size and content.
Expert Zone
1
The order of elements in a set can appear stable in small examples but is not guaranteed and can change between runs or Python versions.
2
Hash collisions can happen but Python handles them internally to maintain performance and correctness.
3
Frozen sets are immutable versions of sets useful as dictionary keys or set elements themselves.
When NOT to use
Avoid sets when you need to preserve order or allow duplicates. Use lists or tuples instead. For key-value pairs, use dictionaries. For immutable collections, use frozenset.
Production Patterns
Sets are used in production to remove duplicates from data, perform fast membership tests, and implement algorithms like graph traversal or filtering unique items efficiently.
Connections
Hash tables
Sets are implemented using hash tables internally.
Understanding hash tables helps explain why sets are fast and why elements must be immutable.
Database unique constraints
Sets enforce uniqueness like unique constraints in databases.
Knowing how sets enforce uniqueness helps understand how databases prevent duplicate records.
Mathematical sets
Python sets model the mathematical concept of sets with unique elements and no order.
Recognizing this connection helps apply mathematical set operations in programming.
Common Pitfalls
#1Creating an empty set using {} instead of set()
Wrong approach:empty = {}
Correct approach:empty = set()
Root cause:Confusing set literal syntax with dictionary literal syntax.
#2Adding a list to a set causing an error
Wrong approach:my_set = {1, 2} my_set.add([3, 4])
Correct approach:my_set = {1, 2} my_set.add((3, 4)) # use tuple instead
Root cause:Not understanding that set elements must be immutable and hashable.
#3Expecting sets to keep insertion order
Wrong approach:s = {'a', 'b', 'c'} print(list(s)[0]) # expecting 'a'
Correct approach:Use a list if order matters: lst = ['a', 'b', 'c']
Root cause:Misunderstanding that sets are unordered collections.
Key Takeaways
Sets are collections that store unique, unordered items.
Use set() to create an empty set; {} creates an empty dictionary.
Sets automatically remove duplicates from any iterable you convert.
Only immutable and hashable items can be elements of a set.
Sets use hashing internally for fast membership checks and uniqueness enforcement.