Convert List to Set Python

Learn via video courses
Topics Covered

Python utilizes data structures like lists and sets, each serving distinct purposes. Lists excel in mathematical operations, aiding in the search for elements, particularly with duplicates. Sets, on the other hand, offer functionality for operations like unions and intersections. Efficiently managing and organizing data is crucial in Python. Converting a list to a set becomes valuable when removing duplicates or performing set operations. This article explores methods to achieve a list-to-set conversion in Python, showcasing the versatility of these data structures in Python programming.

How to Convert a List to Set in Python?

  • Data structure offers a method for effectively managing, organizing, and storing data.
  • Data structures make it simple to navigate the data elements. Data structures offer productivity, reuse, and abstraction.
  • We can think of lists as a box containing a finite number of elements. Similarly, a set can be considered a box where no duplicate elements are allowed.
  • In the example below, we can see a list ([10, 50, 10, 70, 10, 50, 20, 70]) getting converted into a list, which comprises only the unique elements of the list. Introduction to Lists and Sets in Python
  • A list allows any number of variables in it, but a set doesn't allow duplicates.
  • A compelling use case of sets is when we are given a list and have to return only the unique elements.
  • A list looks something like this:
  • A set looks something like this:
  • The most striking difference between a list and a set is list is represented by [], whereas a set is represented by {}.
  • Hence, this article will unravel the methods of converting lists into sets.

Method 1: Using the set() function to convert the list to a set in Python

  • The set() function will help us to convert a list to a set.
  • The order of the set would be different, as a set displays the items in random order. Syntax:

Example:

Output

As you can see, the set only contains unique elements from the list.

Note: A list can contain any data type; it can be a list of strings, integers, floats, etc.

Method 2: Using for-loop to convert list to set in Python

  • In this technique, we access each list element using Python's for-loop method before adding it to a set using the add() function. Syntax:

The set automatically handles the repeated values.

Output

Method 4: Using Set comprehension to convert a list to set

  • Set Comprehension is a method in Python from which we can generate sets in a single line.
  • In Python, comprehensions are used to create new sequences from existing ones. You can read more about comprehension here
  • Programmers use set comprehension when creating an extra/dummy set in their code.
  • In set comprehension, we use an in-place for loop to convert a list into a set.

Output:

Time Complexity to convert list to set

  • The general time complexity to convert a list into a set is O(N), where NN represents the number of elements in the list. A question might arise as to how we know if the available time complexity is O(N). We'll figure it out!
  • The time complexity in a set to add/delete elements is O(1). Assuming the number of elements in our list to be NN, we'd have to traverse every element in the list.
  • Hence, the time complexity will be calculated as follows:
  • The execution time of these 4 methods are compared below:
    MethodExecution Time
    Set Comprehension0.00018
    Using set()0.00016
    Using for-loop0.00019
    Using dictionary0.00021

Similar Topics

Conclusion

In this article, we have covered the following:

  • Utility of lists and sets, how they are used, and their purpose.
  • Difference between sets and lists, along with comprehensive examples.
  • Methods to convert list to set.
  • Time Complexity of different methods and a thorough explanation regarding the time complexity.