Python Reference Library →

Python set() Function – Create a Set from Iterable

Let’s look at set() function – create a set from iterable

  1. Notice

    set() constructs a mutable collection of unique hashable elements. Do not rely on set iteration order.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = [3, 2, 2]
print(sorted(set(items)))
Live state

Top-level scope

items→Object 1
Object 1 · list[3, 2, 2]
This step

set() constructs a mutable collection of unique hashable elements. Do not rely on set iteration order.

Picture set() function – create a set from iterable

set() constructs a mutable collection of unique hashable elements. Do not rely on set iteration order.

Image brief

Alt text
A diagram for Python set() Function – Create a Set from Iterable. Instructions: items = [3, 2, 2] print(sorted(set(items))). Displayed result: [2, 3]. The final example bindings are items --> object 1 (list). object 1: [3, 2, 2]
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python set() Function – Create a Set from Iterable". Teaching focus: set() constructs a mutable collection of unique hashable elements. Do not rely on set iteration order. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS items = [3, 2, 2] print(sorted(set(items))) | v DISPLAYED RESULT [2, 3] FINAL BINDINGS items --> object 1 (list) object 1: [3, 2, 2] Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python set() Function – Create a Set from Iterable. Instructions: items = [3, 2, 2] print(sorted(set(items))). Displayed result: [2, 3]. The final example bindings are items --> object 1 (list). object 1: [3, 2, 2]

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = [3, 2, 2]
print(sorted(set(items)))
Live state

Top-level scope

items→Object 1
Object 1 · list[3, 2, 2]
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[2, 3]

What do you expect this program to print?

What does this example display?

items = [3, 2, 2]
print(sorted(set(items)))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[2, 3]

Follow the printed values in order. The output is: [2, 3]

More examples and reference

Python set() Function

The set() function in Python is used to create a set object. A set is an unordered collection of unique items — meaning it automatically removes duplicates.

Syntax

set([iterable])

Parameters:

  • iterable – (Optional) Any iterable such as list, tuple, string, or dictionary keys.

Returns:

  • A new set object containing unique elements.

Example 1: Convert a List to a Set

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers)
{1, 2, 3, 4, 5}

Example 2: Create a Set from a String

text = "banana"
letters = set(text)
print(letters)
{'b', 'a', 'n'}

Note: The order may vary because sets are unordered.

Example 3: Empty Set

empty = set()
print(empty)
set()

Warning: Using {} creates an empty dict, not a set.

Use Cases of set()

  • Remove duplicates from a list
  • Check for unique items
  • Perform set operations like union, intersection, difference

Example 4: Removing Duplicates Using set()

data = ["apple", "banana", "apple", "cherry"]
unique_fruits = list(set(data))
print(unique_fruits)
['banana', 'cherry', 'apple']

(Order may vary)

Common Mistakes

  • {} is an empty dictionary, not a set — use set() for an empty set
  • Sets do not support indexing or slicing
  • Items in a set must be hashable (e.g., no lists)

Interview Tip

set() is often used in coding problems involving uniqueness, duplicates, or fast lookups without ordering.

Summary

  • set() converts iterables into a set of unique elements
  • Used to remove duplicates or perform set operations
  • Unordered and unindexed collection
  • Use set(), not {}, to create an empty set

Practice Problem

Write a program to read 5 names from the user and display only the unique names entered.

names = []
for _ in range(5):
    names.append(input("Enter a name: "))
print("Unique names:", set(names))

What to remember

Connect what you noticed in the example with the idea below.

Remember

set() constructs a mutable collection of unique hashable elements. Do not rely on set iteration order.

Watch out for this idea

set() retains duplicate elements.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →