Python Sets – Creation Methods & Rules (set() vs {})

Before working with Python sets, it is important to understand how they are created. Learning the available creation methods helps in building sets correctly and understanding how they handle data.

Python set creation can be done using two common approaches:

  1. Curly braces {}: Useful when values are already known.
  2. set() constructor: Commonly used when converting another iterable such as a list, tuple, or string into a set.

Understanding these creation methods is important because sets follow specific rules, such as storing only unique values and not maintaining insertion order.

Getting started with sets? Explore the complete Python Sets guide to learn set creation, rules, operations, and practical examples.

Methods and Syntax of Python Set Creation

The method you choose depends on whether the values are already available or need to be converted from another iterable.

Method 1: Python Set Creation Using Curly Braces {}

Curly braces are the simplest way to create a set when values are already known.

Syntax

set_name = {value1, value2, value3}

Example:

fruits = {"apple", "banana", "orange"}
print(fruits)


# Output (order may vary):
{'apple', 'banana', 'orange'}

This method is short, simple, and easy to read.

Method 2: Python Set Creation Using set()

The set() constructor is useful for creating an empty set or converting another iterable into a set.

Syntax

set_name = set(iterable)

The iterable can be a list, tuple, string, or another set.

Example:

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


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

The set() function converts the list into a set.

Difference Between set() and {}

Method When to Use Example
{} When set values are already known {1, 2, 3}
set() When creating an empty set or converting a list, tuple, string, or another iterable into a set set([1, 2, 3])

Curly braces are simpler when creating sets with known values, while set() is useful for creating empty sets or converting existing data into sets.

Important Rules for Python Set Creation

Python sets follow a few important rules.

  • Sets store only unique values
  • Duplicate elements are removed automatically
  • Sets are unordered collections
  • Sets do not support indexing
  • Mutable values like lists cannot be stored inside sets
  • Set elements must be immutable types like numbers, strings, or tuples

Examples of Python Set Creation

Example 1: Duplicate Values Are Removed Automatically

values = {1, 2, 2, 3, 3, 4}
print(values)


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

Explanation: Duplicate values are automatically removed in a set.

Example 2: Creating an Empty Set

Creating an empty set is slightly different.

empty_set = set()
print(empty_set)


# Output:
set()

Explanation: Using {} creates an empty dictionary, not an empty set.

Example 3: Checking the Type of {}

data = {}
print(type(data))


# Output:
<class 'dict'>

Explanation: Empty curly braces create a dictionary by default.

Example 4: Creating a Set from a String

letters = set("python")
print(letters)


# Output (order may vary):
{'t', 'h', 'p', 'n', 'o', 'y'}

Explanation: Each character becomes a separate unique element in the set.

Example 5: Creating a Set from a Tuple

numbers = set((10, 20, 30, 40))
print(numbers)


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

Explanation: A tuple can be converted into a set using the set() constructor.

Common Mistakes in Python Set Creation

These are some common mistakes that beginners often make while creating Python sets:

  • Using {} to create an empty set
  • Expecting sets to keep elements in the same order
  • Trying to add lists or dictionaries inside a set
  • Forgetting that duplicate values are removed automatically

Key Takeaways: Set Creation and Rules

Here is a quick recap of the important points about Python set creation and rules:

  • Python sets can be created using {} or set()
  • Curly braces are useful when values are already known
  • set() is useful for creating empty sets and converting lists, tuples, strings, and other iterables
  • Sets store only unique values
  • Duplicate elements are removed automatically
  • Sets are unordered and do not support indexing
  • set() must be used to create an empty set

Leave a Comment

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

Scroll to Top