Python set() Function
set()It is a built-in function in Python used to create a mutable set.
A set is an unordered collection of unique elements, similar to the concept of a set in mathematics.set()The function can create a set from other iterable objects, commonly used for deduplication, set operations, and other scenarios.
Word Definition: setMeans "set", an unordered collection of unique elements in Python.
Basic Syntax and Parameters
Syntax Format
set(iterable)
Parameter Description
- Parameter iterable:
- Type: Iterable object
- Description: The iterable object to be converted into a set.
Function Description
- Return Value: Returns a mutable set.
- Special Value: Returns an empty set if no argument is passed.
set()(Note:{}an empty dictionary is created).
Examples
Example 1: Create a Set
Examples
# Create from a list (automatically deduplicates)
lst = [1, 2, 2, 3, 3, 3, 4]
s = set(lst)
print(s) # Output: {1, 2, 3, 4}
# Create from a string
s = set("hello")
print(s) # Output: {'h', 'e', 'l', 'o'}
# Create from a tuple
t = (1, 2, 3, 2, 1)
s = set(t)
print(s) # Output: {1, 2, 3}
# Create from a dictionary (keys only)
d = {"a": 1, "b": 2, "c": 3}
s = set(d)
print(s) # Output: {'a', 'b', 'c'}
# Empty set
s = set()
print(s) # Output: set()
print(type(s)) # Output: <class 'set'>
lst = [1, 2, 2, 3, 3, 3, 4]
s = set(lst)
print(s) # Output: {1, 2, 3, 4}
# Create from a string
s = set("hello")
print(s) # Output: {'h', 'e', 'l', 'o'}
# Create from a tuple
t = (1, 2, 3, 2, 1)
s = set(t)
print(s) # Output: {1, 2, 3}
# Create from a dictionary (keys only)
d = {"a": 1, "b": 2, "c": 3}
s = set(d)
print(s) # Output: {'a', 'b', 'c'}
# Empty set
s = set()
print(s) # Output: set()
print(type(s)) # Output: <class 'set'>
Expected output:
{1, 2, 3, 4}
{'h', 'e', 'l', 'o'}
{1, 2, 3}
{'a', 'b', 'c'}
set()
<class 'set'>
Code analysis:
- Sets automatically remove duplicate elements.
- Sets are unordered; the order of elements may vary.
- Note:
{}An empty dictionary is created, not an empty set.
Example 2: Set Operations
Examples
# Create two sets
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
# Union
print(A | B) # Output: {1, 2, 3, 4, 5, 6}
print(A.union(B)) # Output: {1, 2, 3, 4, 5, 6}
# Intersection
print(A & B) # Output: {3, 4}
print(A.intersection(B)) # Output: {3, 4}
# Difference (A - B)
print(A - B) # Output: {1, 2}
print(A.difference(B)) # Output: {1, 2}
# Symmetric difference
print(A ^ B) # Output: {1, 2, 5, 6}
print(A.symmetric_difference(B)) # Output: {1, 2, 5, 6}
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
# Union
print(A | B) # Output: {1, 2, 3, 4, 5, 6}
print(A.union(B)) # Output: {1, 2, 3, 4, 5, 6}
# Intersection
print(A & B) # Output: {3, 4}
print(A.intersection(B)) # Output: {3, 4}
# Difference (A - B)
print(A - B) # Output: {1, 2}
print(A.difference(B)) # Output: {1, 2}
# Symmetric difference
print(A ^ B) # Output: {1, 2, 5, 6}
print(A.symmetric_difference(B)) # Output: {1, 2, 5, 6}
Expected output:
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6}
{3, 4}
{3, 4}
{1, 2}
{1, 2}
{1, 2, 5, 6}
{1, 2, 5, 6}
Sets support rich set operations: union, intersection, difference, and symmetric difference.
Other Extensions
Python3 Built-in Functions