set()
function or curly braces {}
. You can also create a set from an iterable object like lists, tuples, or strings using the set()
function.# Creating sets
my_set1 = {1, 2, 3, 4}
my_list = [1, 2, 2, 3, 4]
my_set2 = set(my_list)
# Performing set operations
my_set1.union(my_set2) # Union
my_set1 & my_set2 # Intersection
my_set1 - my_set2 # Difference
my_set1 ^ my_set2 # Symmetric difference
in
and not in
operators to check if an element is present in a set.# Membership testing
3 in my_set1 # True
5 not in my_set1 # True
add()
, update()
, or union()
methods to add elements to a set.# Adding elements
my_set1.add(5) # Adding one element
my_set1.update([6, 7]) # Adding multiple elements
remove()
, discard()
, or pop()
methods to remove elements from a set.# Removing elements
my_set1.remove(3) # Removes exactly one occurrence of the element
my_set1.discard(2) # Removes an element if it exists, does nothing otherwise
my_set1.pop() # Removes and returns an arbitrary element from the set
list()
function to convert a set back into a list, or the set()
function to create a new set from a list.# Converting between sets and lists
my_list = list(my_set1) # Converting set to list
my_set3 = set([8, 9, 4]) # Creating a new set from a list
my_set1 = {1, 2, 3}
my_set2 = {2, 4, 5}
union_result = my_set1.union(my_set2)
intersection_result = my_set1 & my_set2
symmetric_difference_result = my_set1 ^ my_set2
print("Union:", union_result)
print("Intersection:", intersection_result)
print("Symmetric Difference:", symmetric_difference_result)
my_set1 = {1, 2, 3}
my_set1.add(4)
my_set1.update([5, 6])
my_set1.remove(2)
print("Set after adding and removing elements:", my_set1)
What causes it: Attempting to access an element that doesn't exist in the set using the pop()
method.
# Bad code example that triggers a KeyError
my_set1 = {1, 2, 3}
print(my_set1.pop(4))
Error message:
KeyError: 4
Solution: Use my_set1.pop()
to remove and return an arbitrary element from the set if you don't know the exact index of the element you want to remove.
Why it happens: The pop()
method requires a valid index, but in this case, we are trying to access an invalid index (4) that doesn't exist in the set.
How to prevent it: Always ensure you know the elements within your set and use their indices when using the pop()
method, or remove elements using the remove()
or discard()
methods instead if you don't care about the returned value.
What causes it: Trying to perform an operation with incompatible data types. For example, adding a non-hashable object like a list to a set.
# Bad code example that triggers a TypeError
my_set1 = {1, 2, 3}
my_list = [1, 2, 3, "apple"]
my_set1.add(my_list)
Error message:
TypeError: unhashable type 'list'
Solution: Convert the list to a set or a tuple before adding it to another set.
Why it happens: Sets require their elements to be hashable, but lists are not hashable by default because they can have duplicate values and an arbitrary order.
How to prevent it: Make sure that all elements you add to a set are either immutable (like integers, floats, or tuples) or mutable objects that don't contain duplicate values (like sets). If you need to work with a list, convert it to a set or tuple before adding it to another set.
set()
function or curly braces {}
.