How to convert list to set Python (4 Easy Ways)

In this article, we will solve a problem statement of 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.
Understanding the Problem Statement
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 a set from a list we will get all distinct elements of a list in the set.
Also read: How to convert set to list in Python
Difference between List and Set in Python
List | Set |
---|---|
The list allows duplicate elements | The set doesn’t allow duplicate elements |
The list is an ordered sequence | The set is an unordered sequence |
The elements in the list are present in square brackets [ ]. | The elements in the list are present in curly brackets { }. |
Using the list we can implement ArrayList, LinkedList, Vector, Stack | Using the set we can implement HashSet and LinkedHashSet. |
How to Convert list to set Python
In Python, we can convert list to set in 4 different ways.
- Using set() method
- Using For loop and set.add() method
- Using set comprehension
- Using dict.fromkey() method
All the above ways are discussed below in detail.
Method 1- Using set()
The best 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 and set add() method
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
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'}
Method 4- Using dict.fromkey()
In this method, we will be using the dictionary fromkeys() method. To know about dict.fromkey() method you can visit here.
The only disadvantage of this method is we don’t get set in an orderly manner.
Python Code
# a is defined list
a = [1,2,3,'seeker',3,7.5]
# Using dict.fromkeys() method
x = list(dict.fromkeys(a))
converted_set = set(x)
print(converted_set)
Output:
{1, 2, 3, 7.5, 'seeker'}
Time Complexity to convert list to set in Python?
The time complexity to convert list to set is Big O(N), where N is the number of elements present in the list, i.e it is linear to the elements present in the list. This is because the complexity of set operations to add/delete an element is O(1). Hence while converting a list of N elements into a set, we require such N operations, which will have the complexity of N * O(1) i.e O(N * 1) = O(N).
Above, we have discussed 4 methods that can help you to convert list to set in python. But which method is best from above with respect to time 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 Name | Time of Execution |
Using set() Function | 0.00016019 |
Using for loop | 0.00019580 |
Using Set Comprehension | 0.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() method, for loop, and set comprehension. The Time complexity for converting the list to set is O(N). We can also conclude the best way to convert list to set is by using the set() method.