In Python, Set is an unordered collection of unique elements. It can store only mutable elements collection.
Set are mutable, meaning we can add or remove elements from them, but they doesn't support indexing or slicing like lists or tuples. Sets allow us to loop through the set.
Set are useful for tasks that require uniqueness and membership testing.
Here's how to work with set in Python:
# declare set object and values comma-separated inside {} curly braces. your_set_var = {value1, value2} # declare set object and values comma-separated inside set functions [] square brackets. your_set_var_name = set([value1, value2])
We can create a set by enclosing elements within curly braces {} or by using the "set()" constructor.
# set using curly braces your_set = {1, 2, 3, 4, 5, 6, 7, 8, 9} print(your_set) # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9} print(type(your_set)) # Output: <class 'set'> # using set() constructor your_set_using_func = set([1, 2, 3, 4, 5, 6, 7, 8, 9]) for val in your_set_using_func: print(val) # Output: 1 2 3 4 5 6 7 8 9 print(type(your_set_using_func)) # Output: <class 'set'>
# set using curly braces your_set = {} print(your_set) # Output: {} print(type(your_set)) # Output: <class 'dict'> # using set() constructor your_set_using_func = set([]) print(your_set_using_func) # Output: set() print(type(your_set_using_func)) # Output: <class 'set'>
Creating a set with empty values inside {} curly braces will change the type of `set` class to a `dict` class.
your_set = {1, 2, 3, 4, 5, 6, 7, 8, 9} your_set.add(10) print(your_set) # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} your_set.add(10) # it will print same result, becoz set accept unique values only print(your_set) # Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
We can add elements to a set using the "add()" method and the set object stores unique values.
your_set = {1, 2, 3, 4, 5, 6, 7, 8, 9} your_set.remove(8) print(your_set) # Output: {1, 2, 3, 4, 5, 6, 7, 9} your_set.discard(5) print(your_set) # Output: {1, 2, 3, 4, 6, 7, 9}
We can remove elements from a set using the "remove()" or "discard()" method.
set1 = {1, 2, 3, 4, 5, 6} set2 = {3, 4, 5, 6, 7, 8} # Union union_set = set1.union(set2) print(union_set) # Output: {1, 2, 3, 4, 5, 6, 7, 8} # Intersection intersection_set = set1.intersection(set2) print(intersection_set) # Output: {3, 4, 5, 6} # Symmetric Difference symmetric_difference_set = set1.symmetric_difference(set2) print(symmetric_difference_set) # Output: {1, 2, 7, 8} # Difference difference_set = set1.difference(set2) print(difference_set) # Output: {1, 2}
If the element is not present, "remove()" will raise a "KeyError", while "discard()" will not.
squares_set = {x**2 for x in range(1, 8)} print(squares_set) # Output: {1, 4, 9, 16, 25, 36, 49}
Set supports various operations such as union, intersection, difference, and symmetric difference.
your_set = {1, 2, 3, 4, 5, 6, 7, 8, 9} print(5 in your_set) # Output: True print(8 in your_set) # Output: True print(10 in your_set) # Output: False
Set comprehensions provide a concise way to create a set.
We can check value exists in the Set using "in" operator.
Set is commonly used in tasks that required uniqueness, such as removing duplicate elements from a list, and performing set operations like union, intersection, and difference.