• How to Convert List to Set Python

    convert list to set python

    In this article, we will be solving a problem 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’}

    Method 4- Using dict.fromkey() convert list to set Python

    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?

    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 NameTime of Execution
    Using set() Function0.00016019
    Using for loop0.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 function, 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. 

    About Author
    Siddhesh Jadhav

    I am Passionate Computer Engineer. Writing articles about programming problems and concepts allows me to follow my passion for programming and helping others.