Let’s explain it step-by-step:
What happens here?
Line 1
s = {1, 2, 3}A set is created with three integers.
Line 2
s.add([4, 5])Here you are trying to add a list [4, 5] into a set.
๐ But sets can only store hashable (immutable) objects.
int, float, str, tuple → hashable ✅
list, dict, set → not hashable ❌
A list is mutable, so Python does not allow it inside a set.
So this line raises an error:
TypeError: unhashable type: 'list'Line 3
print(s)This line is never executed, because the program stops at the error in line 2.
❌ Final Output
There is no output — instead you get:
TypeError: unhashable type: 'list'✅ How to fix it?
If you want to store 4 and 5 as a group inside the set, use a tuple:
Output (order may vary):
{1, 2, 3, (4, 5)}Or if you want to add them as individual elements:
Output:
{1, 2, 3, 4, 5}๐ Key Concept
Sets only allow immutable (hashable) elements.
That’s why lists can’t go inside sets — but tuples can


0 Comments:
Post a Comment