Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Sets – Complete Guide for Beginners

A Set is one of Python's built-in data types used to store multiple items in a single variable. Sets are useful when you need to store unique values and perform mathematical set operations such as union, intersection, and difference.

Unlike lists and tuples, sets do not allow duplicate values and do not maintain a specific order of elements.

Python sets are widely used in data analysis, database operations, filtering duplicates, and membership testing.


What is a Set in Python?

A set is:

  • Unordered
  • Unchangeable (items cannot be modified directly)
  • Does not allow duplicate values
  • Mutable (you can add or remove items)

Sets are created using curly braces {}.

Example

fruits = {"apple", "banana", "orange"}

print(fruits)

Output

{'apple', 'banana', 'orange'}

Why Use Sets?

Sets are useful when:

  • Removing duplicate values
  • Checking membership quickly
  • Performing mathematical operations
  • Working with unique collections of data

Example:

numbers = {1, 2, 3, 2, 1, 4}

print(numbers)

Output:

{1, 2, 3, 4}

Notice that duplicates are automatically removed.


Creating a Set

Using Curly Braces

colors = {"red", "green", "blue"}

print(colors)

Using the set() Constructor

colors = set(("red", "green", "blue"))

print(colors)

Output:

{'red', 'green', 'blue'}

Set Data Types

A set can contain different data types.

data = {
    "Python",
    100,
    True,
    3.14
}

print(data)

Output:

{True, 100, 3.14, 'Python'}

Duplicate Values Not Allowed

fruits = {
    "apple",
    "banana",
    "apple",
    "orange"
}

print(fruits)

Output:

{'apple', 'banana', 'orange'}

Python automatically removes duplicates.


Length of a Set

Use the len() function.

fruits = {
    "apple",
    "banana",
    "orange"
}

print(len(fruits))

Output:

3

Access Set Items

Since sets are unordered, you cannot access items by index.

Instead, use a loop.

fruits = {
    "apple",
    "banana",
    "orange"
}

for fruit in fruits:
    print(fruit)

Output:

apple
banana
orange

Order may vary.


Check if Item Exists

Use the in keyword.

fruits = {
    "apple",
    "banana",
    "orange"
}

print("banana" in fruits)

Output:

True

Add Items to a Set

Use the add() method.

fruits = {
    "apple",
    "banana"
}

fruits.add("orange")

print(fruits)

Output:

{'apple', 'banana', 'orange'}

Add Multiple Items

Use update().

fruits = {
    "apple",
    "banana"
}

fruits.update(
    ["orange", "mango"]
)

print(fruits)

Output:

{'apple', 'banana', 'orange', 'mango'}

Remove Items

Using remove()

fruits = {
    "apple",
    "banana",
    "orange"
}

fruits.remove("banana")

print(fruits)

Output:

{'apple', 'orange'}

If the item does not exist, Python raises an error.


Using discard()

fruits.discard("banana")

No error occurs if the item is missing.


Remove Random Item

Use pop().

fruits = {
    "apple",
    "banana",
    "orange"
}

item = fruits.pop()

print(item)
print(fruits)

Output varies because sets are unordered.


Clear a Set

fruits = {
    "apple",
    "banana"
}

fruits.clear()

print(fruits)

Output:

set()

Delete a Set

fruits = {
    "apple",
    "banana"
}

del fruits

The set is completely removed from memory.


Loop Through a Set

numbers = {
    1, 2, 3, 4, 5
}

for num in numbers:
    print(num)

Output:

1
2
3
4
5

Order may vary.


Join Sets

Union Method

The union() method combines sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

result = set1.union(set2)

print(result)

Output:

{1, 2, 3, 4, 5}

Union Operator

set1 = {1, 2}
set2 = {3, 4}

print(set1 | set2)

Output:

{1, 2, 3, 4}

Set Intersection

Returns common values.

set1 = {1, 2, 3}
set2 = {2, 3, 4}

print(
    set1.intersection(set2)
)

Output:

{2, 3}

Intersection Operator

print(set1 & set2)

Output:

{2, 3}

Set Difference

Returns elements only in the first set.

set1 = {1, 2, 3}
set2 = {2, 3, 4}

print(
    set1.difference(set2)
)

Output:

{1}

Difference Operator

print(set1 - set2)

Output:

{1}

Symmetric Difference

Returns elements that exist in only one set.

set1 = {1, 2, 3}
set2 = {3, 4, 5}

print(
    set1.symmetric_difference(set2)
)

Output:

{1, 2, 4, 5}

Symmetric Difference Operator

print(set1 ^ set2)

Output:

{1, 2, 4, 5}

Copy a Set

set1 = {
    1, 2, 3
}

set2 = set1.copy()

print(set2)

Output:

{1, 2, 3}

Nested Sets

Regular sets cannot contain other sets because sets are mutable.

This causes an error:

myset = {
    {1, 2},
    {3, 4}
}

Use frozenset instead.

myset = {
    frozenset({1, 2}),
    frozenset({3, 4})
}

print(myset)

Real-World Example: Remove Duplicates

emails = [
    "user1@gmail.com",
    "user2@gmail.com",
    "user1@gmail.com",
    "user3@gmail.com"
]

unique_emails = set(emails)

print(unique_emails)

Output:

{
 'user1@gmail.com',
 'user2@gmail.com',
 'user3@gmail.com'
}

Common Set Methods

MethodDescription
add()Add one item
update()Add multiple items
remove()Remove item
discard()Remove without error
pop()Remove random item
clear()Remove all items
copy()Create copy
union()Combine sets
intersection()Common values
difference()Values not in second set
symmetric_difference()Unique values from both sets

Advantages of Sets

  • Automatically remove duplicates
  • Fast membership testing
  • Efficient mathematical operations
  • Easy data filtering
  • Useful in data science and analytics

Common Mistakes

Using Indexes

Incorrect:

fruits = {
    "apple",
    "banana"
}

print(fruits[0])

Output:

TypeError

Sets do not support indexing.


Adding Duplicate Values

numbers = {1, 2, 2, 3}

print(numbers)

Output:

{1, 2, 3}

Duplicates are removed automatically.


Best Practices

  • Use sets when uniqueness matters.
  • Use sets for fast lookups.
  • Use set operations instead of complex loops.
  • Convert lists to sets to remove duplicates.
  • Use discard() instead of remove() when uncertain an item exists.

Conclusion

Python Sets are powerful data structures designed for storing unique values and performing fast mathematical operations. They are ideal for removing duplicates, comparing collections, and checking membership efficiently.

Understanding sets is essential for Python developers because they simplify data processing tasks and improve application performance. 




Post a Comment

0 Comments