What is the set() Constructor in Python?
The set() constructor is a built-in Python function used to create a set from an iterable. It takes values from a sequence like a list, tuple, or string and converts them into a set.
Duplicates are removed automatically during this process. The result is an unordered collection that stores only unique elements.
Python set() Constructor: Syntax & Examples
Before moving ahead, here’s the basic syntax of the set() constructor.
Syntax
set(iterable)
Parameter Description
| Parameter | Description |
|---|---|
| iterable | Optional. Any iterable like list, tuple, string, etc. If not provided, an empty set is created. |
Return Value
The set() constructor returns a new set containing only unique elements from the iterable. If no value is passed, it returns an empty set.
Python SET – set() Constructor: Practical Examples for All Scenarios
Simple Level Examples
Example 1: Create a set from a list
numbers = set([1, 2, 3, 3, 4])
print(numbers)
Explanation: The list is converted into a set, removing duplicates like 3. The final output keeps only unique elements.
Example 2: Create a set from a string
letters = set("hello")
print(letters)
Explanation: The string is converted into a set, breaking it into individual characters. Duplicate letters like l are removed, and only unique characters remain.
Example 3: Create an empty set
empty_set = set()
print(empty_set)
Explanation: An empty set is created using set(). It starts with no values and can be used later when data needs to be added.
Example 4: Create a set from a tuple
tuple_data = set((10, 20, 10))
print(tuple_data)
Explanation: The tuple is converted into a set, so duplicate values like 10 are removed. Only unique elements like 10 and 20 remain.
Example 5: Create a set from a range object
range_set = set(range(5))
print(range_set)
Explanation: The range is converted into a set of numbers. It generates values from 0 to 4, storing only unique elements.
Medium Level Examples
Example 6: Create set from mixed data types in a list
mixed = set([1, 2.5, "apple", 2.5])
print(mixed)
Explanation: Different data types are combined and converted into a set. Duplicate values like 2.5 are removed, leaving only unique items.
Example 7: Nested iterable handling (only top-level considered)
nested = set([(1, 2), (3, 4), (1, 2)])
print(nested)
Explanation: Tuple pairs are stored in a set, and duplicates like (1, 2) are ignored. Only unique tuples remain in the final output.
High Level Examples
Example 8: Use set constructor in function to remove duplicates
def unique_items(iterable):
return set(iterable)
result = unique_items([1,2,2,3,3,3])
print(result)
Explanation: The function converts a list into a set. Duplicate values are removed, and only unique elements are returned.
Example 9: Creating sets from dictionary keys and values
d = {'a': 1, 'b': 2, 'c': 1}
keys_set = set(d)
values_set = set(d.values())
print(keys_set)
print(values_set)
Explanation: Dictionary keys and values are converted into sets. This extracts only unique elements from both parts of the dictionary.
Use Cases: When to Use the set() Constructor?
The set() constructor comes in handy when working with everyday data in Python.
- Helps remove duplicate values from a list or any sequence
- Useful when converting data into a set for operations like union or intersection
- Can be used to create an empty set for adding values later
- Makes filtering and membership checks faster and more efficient
In practice, it’s often used during data cleaning, preprocessing, and simple comparison tasks where unique values matter.
Key Takeaways: Python set() Constructor
Before wrapping up, here’s a quick look at the main points you should remember from the Python set() constructor.
- The set() constructor is used to create a set from iterables like lists, tuples, and strings.
- It automatically removes duplicate values and keeps only unique elements.
- If no value is provided, it returns an empty set.
- It can easily convert different iterable types into a set structure.
- The resulting set supports common operations like union, intersection, and difference.
- It is useful for handling unique data efficiently in Python programs.
- Overall, it helps simplify data processing where uniqueness matters.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.