How to Convert List to Set Python

convert list to set python

In this article, we will be solving a program statement for how to convert List to Set Python. We will be discussing the steps and python methods required to solve this problem and also the time complexity of the methods used to convert list to set in python.

Before jumping directly into the python program let’s know about the background of the problem statement, python list, and python set. 

What is List in Python? The list is one of the 4 built-in data types in Python used to store data that is mutable, can be ordered, and can be duplicated within the list. The list is created by placing all the elements within the square brackets and separated by commas.

Example : list1 = [1,2,3,'john',7.5,3]

What is Set in Python? Like list, Set is also one of the 4 built-in data types in Python used to store data but cannot have duplicate elements and cannot be modified. The Set is created by placing all the elements within the curly brackets separated by commas.

Example : set1 = {1,2,3,'john',7.5}

Since now we know that set contains only distinct elements, hence when we create set from list we will get all distinct element of a list in the set.

Method 1- Using set() to convert list to set Python

The simplest way to convert list to set in Python is by using the set() function. The set() method is used to convert an iterable element such as a list, dictionary, or tuple into the set. 

Python Code:

# sample_list is defined list
sample_list = [1,2,3,'seeker',3,7.5]

# set() to convert list to set
sample_set  = set(sample_list)
print(sample_set) #printing set

Output:
{1, 2, 3, 7.5, ‘seeker’}

Method 2- For Loop to convert list to set Python

In this method, we use for loop to access each element of a list and add that element in a set by using add() function.

Python Code

# sample_list is defined list
sample_list = [1,2,3,'seeker',3,7.5]

# set() to convert list to set
sample_set = set() # defining set
#using for loop
for i in sample_list:
    #adding i to b
    sample_set.add(i)
print(sample_set)

Output:
{1, 2, 3, 7.5, ‘seeker’}

Method 3- Using Set Comprehension to convert list to set Python

In this method, we will be using set comprehension. You can know about set comprehension from here.

Python Code

# a is defined list
a = [1,2,3,'seeker',3,7.5]

t = {x for x in a} # set comprehension
print(t)

Output:
{1, 2, 3, 7.5, ‘seeker’}

Time Complexity to convert list to set in Python?

Above, we have discussed 3 methods that can help you to convert list to set in python. But which method is best from above with respect to time complexity so that you can practice using that method in upcoming projects or problems.

Below is the table where you check the execution time of each method.

Method NameTime of Execution
Using set() Function0.00016019
Using for loop0.00019580
Using Set Comprehension0.00018290

From the above table, we can see that using the set() function method takes less time compared to other methods.

Conclusion

Hence we can convert list to set Python by using set function, for loop, and set comprehension. We can also conclude the best way to convert list to set is by using set() method.