Understanding Python’s set() Method: A Comprehensive Guide

In this tutorial, we will delve into the world of Python and explore one of its powerful built-in methods – the set() method. This method is used to create a set in Python, which is an unordered collection of unique elements.

What is a Set?

A set in Python is similar to mathematical sets. It does not record element position or order of insertion and contains no duplicate values. Sets are mutable, meaning they can be changed after creation, but they cannot contain mutable elements such as lists.

The set() Method

The set() method is a built-in function that creates a new set object containing elements from any iterable. The syntax for using the set() method is:


The iterable argument can be any sequence (like string, list) or collection (like dictionary, set) or an iterator object to be converted into a set.

An Example Usage:

    # Creating a set from a list
    my_list = [1, 2, 3, 4, 5]
    my_set = set(my_list)

This will output: {1, 2, 3, 4, 5}

Difference between Set and Other Collections

Sets differ from lists or tuples as they cannot contain duplicate values. This makes them incredibly useful when you want to eliminate repeated items in your sequence/collection.

An Example Usage:

    # Removing duplicates from a list
    my_list = [1, 2, 2, 3, 4, 4, 5]
    my_set = set(my_list)

This will output: {1, 2, 3, 4, 5}


The set() method in Python is a powerful tool that allows you to create sets from any iterable. It’s particularly useful when you need to eliminate duplicate values in your data. We hope this tutorial has helped you understand how to use the set() method effectively.

Keep coding and exploring more with Python!

Leave a Reply

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