A set is an unordered collection of unique elements in Python. It is a built-in data type that is commonly used in various programming tasks, such as data processing, searching, and filtering. Here are some key points to remember about Python sets:
In summary, Python sets are a useful tool for working with unique, unordered collections of data. They provide a variety of useful operations for manipulating and processing data, and they can be used in a variety of contexts, from simple data cleaning tasks to complex algorithmic problems.
There are two ways to create a set in Python:
my_set = {1, 2, 3, 4, 5}
print(my_set)
Output:{1, 2, 3, 4, 5}
my_set = set([1, 2, 3, 4, 5])
print(my_set)
Output:
{1, 2, 3, 4, 5}
Note that you can also create an empty set using the set() constructor:
empty_set = set()
print(empty_set)
Unordered: Sets are unordered, meaning the elements are not stored in any particular order.
Unique elements: Sets can only contain unique elements, which means that duplicate items are automatically removed.
Mutable: Sets are mutable, so you can add or remove elements after they are created.
Iterable: Sets are iterable, meaning you can loop over them and perform operations on each element.
Mathematical operations: Sets support various operations such as union, intersection, difference, and symmetric difference.
Hashable elements: Set elements must be hashable, which means that they must have a hash value that does not change during their lifetime.
Not indexable: Sets are not indexable, meaning you cannot access elements by their position in the set.
No duplicates: If you try to add a duplicate element to a set, it will not be added because sets only contain unique elements.
No order: If you try to access an element by its index in a set, you will get a TypeError because sets are unordered.
Overall, sets are a useful data structure in Python for storing and manipulating unique, unordered collections of data. They have many built-in operations that can be used to perform set operations, and they are efficient for many common use cases.
Here are some examples to illustrate the properties of Python sets:
my_set = {3, 1, 4, 1, 5} # Create a set with unordered elements
print(my_set)
# Output: {1, 3, 4, 5}
Note that the order of the elements in the set is not the same as the order they were defined in.
my_set = {3, 1, 4, 1, 5} # Create a set with duplicate elements
print(my_set)
# Output: {1, 3, 4, 5}
Note that the duplicate element “1” was automatically removed from the set.
my_set = {1, 2, 3}
my_set.add(4) # Add an element to the set
print(my_set)
# Output: {1, 2, 3, 4}
my_set.remove(2) # Remove an element from the set
print(my_set)
# Output: {1, 3, 4}
Note that you can add and remove elements from the set after it is created.
my_set = {1, 2, 3, 4}
for element in my_set:
print(element)
# Output: 1 2 3 4
How you can loop over the elements of a set using a for loop.
my_set = {‘apple’, ‘banana’, ‘orange’, ‘pear’}
for element in my_set:
print(element)
# Output: ‘orange’, ‘apple’, ‘pear’, ‘banana’
Note that you can loop over the elements of a set using a for loop.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1.union(set2)) # Union of set1 and set2
# Output: {1, 2, 3, 4, 5, 6}
print(set1.intersection(set2)) # Intersection of set1 and set2
# Output: {3, 4}
print(set1.difference(set2)) # Difference between set1 and set2
# Output: {1, 2}
print(set1.symmetric_difference(set2)) # Symmetric difference between set1 and set2
# Output: {1, 2, 5, 6}
Note that sets have built-in methods for performing common set operations, such as union, intersection, difference, and symmetric difference.
my_set = {(1, 2), “hello”, 3.14} # Create a set with hashable elements
print(my_set)
# Output: {(1, 2), ‘hello’, 3.14}
Note that the elements of a set must be hashable, which means they must have a hash value that does not change during their lifetime.
Immutable objects like tuples, strings, and numbers are hashable.
my_set = {1, 2, 3}
print(my_set[0]) # Try to access an element by index
# Output: TypeError: ‘set’ object is not subscriptable
Note that if you try to add a duplicate element to a set, it will not be added because sets only contain unique elements.
Example:
my_set = {‘apple’, ‘banana’, ‘orange’}
print(my_set[0]) # Try to access an element by index
# Output: TypeError: ‘set’ object is not subscriptable
Note that sets are not indexable, meaning you cannot access elements by their position in the set.
my_set = {‘banana’, ‘apple’, ‘orange’, ‘pear’}
print(my_set)
# Output: {‘orange’, ‘apple’, ‘pear’, ‘banana’}
Note that the order of the elements in the set is not the same as the order they were defined in.
my_set = {‘apple’, ‘banana’, ‘orange’, ‘banana’}
print(my_set)
# Output: {‘apple’, ‘banana’, ‘orange’}
Note that the duplicate element “banana” was automatically removed from the set.
my_set = {‘apple’, ‘banana’, ‘orange’}
my_set.add(‘banana’)
print(my_set)
# Output: {‘apple’, ‘banana’, ‘orange’}
Note that if you try to add a duplicate element to a set, it will not be added because sets only contain unique elements.
Python sets have many uses in programming. Here are some common use cases:
my_list = [1, 2, 2, 3, 3, 3]
my_set = set(my_list)
new_list = list(my_set)
print(new_list) # Output: [1, 2, 3]
my_set = {‘apple’, ‘banana’, ‘orange’}
print(‘banana’ in my_set) # Output: True
print(‘pear’ in my_set) # Output: False
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1.union(set2)) # Union of set1 and set2
# Output: {1, 2, 3, 4, 5, 6}
print(set1.intersection(set2)) # Intersection of set1 and set2
# Output: {3, 4}
print(set1.difference(set2)) # Difference between set1 and set2
# Output: {1, 2}
print(set1.symmetric_difference(set2)) # Symmetric difference between set1 and set2
# Output: {1, 2, 5, 6}
my_list = [1, 2, 2, 3, 3, 3]
unique_count = len(set(my_list))
print(unique_count) # Output: 3
numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
even_numbers = {x for x in numbers if x % 2 == 0}
print(even_numbers) # Output: {2, 4, 6, 8, 10}
Here we use set comprehension to create a new set even_numbers
that contains only the even numbers from the numbers
set.
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = {3, 4, 5}
common_elements = set1.intersection(set2, set3)
print(common_elements) # Output: {3}
Here we use the intersection()
method to find the common elements between sets set1
, set2
, and set3
.
set1 = {1, 2, 3}
set2 = {2, 3}
set3 = {2, 3, 4}
print(set2.issubset(set1)) # Output: True
print(set3.issuperset(set2)) # Output: True
Here we use the issubset()
method to check if set2
is a subset of set1
and the issuperset()
method to check if set3
is a superset of set2
.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
This function can be slow for large values of n
, so we can use a set to cache the previously computed values:
factorial_cache = {0: 1}
def factorial(n):
if n in factorial_cache:
return factorial_cache[n]
else:
result = n * factorial(n-1)
factorial_cache[n] = result
return result
Here we use a dictionary (which is similar to a set) factorial_cache
to store the previously computed factorial values. When computing the factorial of a new value, we first check if the value is already in the cache. If it is, we simply return the cached value. Otherwise, we compute the value and store it in the cache for future use.
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
list3 = [4, 5, 6, 7]
unique_elements = set(list1 + list2 + list3)
print(unique_elements) # Output: {1, 2, 3, 4, 5, 6, 7}
Here we combine the three lists into one using the +
operator, and then convert the resulting list to a set to get the unique elements.