Python set() Constructor: Create Sets from Iterables | Syntax, Use Cases & Examples

What is the Python set() Constructor?

Definition: The Python set() constructor is a built-in function used to create a set from an iterable such as a list, tuple, string, or dictionary.

During conversion, duplicate values are removed automatically, leaving only unique elements in the resulting set.

The set() constructor is also used to create an empty set in Python, making it one of the most commonly used ways to work with sets.

You can jump directly to a quick example to see how the Python set() constructor works.

Also explore its real-world use cases to understand where it is commonly used.

Before exploring practical examples and real-world use cases, it is important to understand the syntax and parameters of the Python set() constructor.

Want to understand how the set() constructor fits into the bigger picture? Visit the Python Sets tutorial for a complete overview.

Syntax, Parameters and Return Value: Python set() Constructor

set(iterable)

Explanation: The set() constructor takes an iterable (like list, tuple, string, or dictionary) and converts it into a set containing only unique elements.

Parameters

ParameterDescription
iterable (optional)An iterable object such as a list, tuple, string, dictionary, or another set whose elements are converted into a set.

Return Value

Return ValueDescription
setReturns a new set containing unique elements from the iterable. If no iterable is provided, it returns an empty set.

Quick Example

The set() constructor can be used with or without an iterable.

print(set([1, 2, 3]))
print(set())

# Output:
{1, 2, 3}
set()

In the first case, the list is converted into a set containing unique elements. In the second case, set() creates an empty set.

How the set() Constructor Works

  • The set() constructor accepts an iterable such as a list, tuple, string, or dictionary.
  • It converts the iterable into a set containing only unique elements.
  • Duplicate values are removed automatically during conversion.
  • If no argument is provided, it creates an empty set.
  • The resulting set is unordered and does not support indexing.

Practical Examples: Python set() Constructor

Note: The order of elements shown in set outputs may vary because sets are unordered collections.

Simple Level Examples

Example 1: Create a set from a list

numbers = set([1, 2, 3, 3, 4])
print(numbers)


# Output:
{1, 2, 3, 4}

Explanation: The set() constructor converts the list into a set. During conversion, duplicate values are removed, so the resulting set contains only unique elements.

Example 2: Create a set from a string

letters = set("hello")
print(letters)

#Output (order may vary):
{'h', 'e', 'l', 'o'}

Explanation: The string is broken into letters, and only unique characters are kept in the set.

Example 3: Create an empty set

empty_set = set()
print(empty_set)


# Output:
set()

Explanation: An empty set is created using set(). It starts with nothing inside.

Example 4: Create a set from a tuple

tuple_data = set((10, 20, 10))
print(tuple_data)


#Output (order may vary):
{10, 20}

Explanation: The tuple is converted into a set, so repeated values like 10 are removed.

Example 5: Create a set from a range object

range_set = set(range(5))
print(range_set)


#Output (order may vary):
{0, 1, 2, 3, 4}

Explanation: range() gives numbers from 0 to 4, and set() stores them as unique values.

Medium Level Examples

Example 6: Create set from mixed data types in a list

mixed = set([1, 2.5, "apple", 2.5])
print(mixed)


#Output (order may vary):
{1, 2.5, 'apple'}

Explanation: Different types are added, but duplicates are removed automatically.

Example 7: Create a Set of Tuples

nested = set([(1, 2), (3, 4), (1, 2)])
print(nested)


#Output (order may vary):
{(1, 2), (3, 4)}

Explanation: Only unique tuples are kept. Duplicate pairs are ignored.

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)


#Output (order may vary):
{1, 2, 3}

Explanation: The function converts the iterable into a set, automatically removing duplicate values.

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)


#Output (order may vary):
{'a', 'b', 'c'}
{1, 2}

Explanation: A dictionary gives only keys when passed to set(). Values are taken separately using values(), and duplicates are removed.

Use Cases: When to Use the set() Constructor

Now let’s explore some common situations where the Python set() constructor becomes useful in real-world programming.

  • 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: set() Constructor

Here are the key points to remember about the Python set() constructor.

  • The set() constructor creates a set from iterables like lists, tuples, and strings.
  • It automatically removes duplicate values and keeps only unique elements.
  • If no argument is passed, it returns an empty set.
  • It can convert different iterable types into a set structure.
  • Sets support operations like union, intersection, and difference.
  • It is useful for handling unique data efficiently in Python programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top