The set()
function in Python is used to create a set object, which is an unordered collection of unique elements. Sets do not allow duplicate elements, and the elements are stored in no particular order. They are useful when you need to store values that should not repeat and do not require ordering.
Syntax:
set([iterable])
iterable
: This is optional. You can pass any iterable (e.g., a list, tuple, or string). If no argument is provided, an empty set is created.
Key Characteristics of a Set:
- Unordered: The elements do not have any index, so you cannot access them by position.
- Unique Elements: Sets automatically remove duplicates.
- Mutable: You can add or remove elements after the set is created.
- No indexing: You can’t access elements by index as you can with lists or tuples.
Examples of Using set()
:
1. Creating a Set from a List
my_list = [1, 2, 3, 3, 4, 4, 5]
my_set = set(my_list)
print(my_set)
Output:
{1, 2, 3, 4, 5}
Explanation: The duplicate values 3
and 4
are automatically removed, leaving only unique values.
2. Creating a Set from a String
my_string = "hello"
my_set = set(my_string)
print(my_set)
Output:
{'h', 'e', 'l', 'o'}
Explanation: The string "hello"
is converted into a set of unique characters.
3. Creating an Empty Set
empty_set = set()
print(empty_set)
Output:
set()
Explanation: An empty set is created.
4. Adding Elements to a Set
my_set = {1, 2, 3}
my_set.add(4) # Adds 4 to the set
print(my_set)
Output:
{1, 2, 3, 4}
Explanation: The add()
method adds a single element to the set.
5. Removing Elements from a Set
my_set = {1, 2, 3, 4}
my_set.remove(3) # Removes element 3
print(my_set)
Output:
{1, 2, 4}
Explanation: The remove()
method removes the specified element from the set. If the element is not found, it raises a KeyError
.
6. Discarding an Element from a Set
my_set = {1, 2, 3, 4}
my_set.discard(3) # Removes 3 if it exists, does nothing if it doesn't
my_set.discard(5) # 5 doesn't exist, so it does nothing
print(my_set)
Output:
{1, 2, 4}
Explanation: The discard()
method removes an element, but it doesn’t raise an error if the element is not found.
7. Set Union (|
)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1 | set2 # Union of set1 and set2
print(result)
Output:
{1, 2, 3, 4, 5}
Explanation: The union of two sets combines all elements from both sets, without duplicates.
8. Set Intersection (&
)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1 & set2 # Intersection of set1 and set2
print(result)
Output:
{3}
Explanation: The intersection of two sets returns the common elements.
9. Set Difference (-
)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1 - set2 # Elements in set1 but not in set2
print(result)
Output:
{1, 2}
Explanation: The difference of two sets returns elements that are in the first set but not in the second.
10. Set Symmetric Difference (^
)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1 ^ set2 # Symmetric difference between set1 and set2
print(result)
Output:
{1, 2, 4, 5}
Explanation: The symmetric difference returns elements that are in either set, but not in both.
Common Set Methods:
add(x)
: Adds elementx
to the set.remove(x)
: Removes elementx
from the set. Raises aKeyError
if the element is not present.discard(x)
: Removes elementx
from the set, but does not raise an error ifx
is not present.pop()
: Removes and returns an arbitrary element from the set (since sets are unordered).clear()
: Removes all elements from the set.copy()
: Returns a shallow copy of the set.
Conclusion:
The set()
function is a powerful tool for working with unique collections of elements. You can perform various set operations like union, intersection, difference, and symmetric difference with simple syntax. Sets are particularly useful when you need to ensure no duplicates are present in your data.