convert set to list in python

6 Best Methods to Convert Set to List in Python? [With Code]

Set to List in Python – Most people, starting their programming career, choose ease of writing code over the optimal one. However, as you progress and start developing the critical code, focusing on optimal code is crucial. Knowing when to use a list over a set data structure is one such important thing for code optimization.

We have two different data stores in this context – List and Set. List is the most commonly used with a lot of in-built functions. However, there are certain use cases where Python Set would be more appropriate to use.

For example, if you want to look up whether the element exists in the data or not, Set is the efficient data structure to use. Whereas in List, it takes O(n) to find out.

Similarly, for accessing data with the index, List is slightly faster and more efficient compared to Set.

Python list is a special and one of the most used data structures. With a wide range of built-in functions and support for various data types, it offers us a great deal of flexibility.

Now let’s understand more about the differences between set vs list with real-life examples and different ways to convert Set to List in Python.

Python List vs Set

Both List and Set data structures in Python are used to store the collection of items. Each of these data store has it’s own unique capabilities and best suited for different types of applications and use cases. The below list depicts the key differences between the lists and sets.

set vs list comparison in python
SetList
1. Set allows only the unique values1. List allows storing duplicate values
2. Set is an unordered data structure2. Data in the list is stored in the ordered sequence
3. Can’t access or insert elements at specified positions3. Can access elements at certain positions using the index
4. Can check if an element exists in data in constant time4. To check if the element is present in the list takes O(N) time

Use cases where Lists are better than Set:

Well, Set is a more optimized data structure to check if the object is present or not and also to maintain the unique elements in the data store. However, if your use case involves using list operations such as slicing and indexing, going with a list is the right choice. Another situation where lists are more appropriate is when you have to store duplicate values.

A few of the real-life examples include –

  1. Spotify Playlist – Another real-world example of List is to create a Spotify playlist where you can add songs to the playlist in order and can play them in a sequence. You can also change the priority of songs you want to listen to.
  2. Shopping cart – Your shopping cart contains a list of items where you need to maintain the order. In this case, lists are useful. Additionally, you might want to add multiple quantities of same item to the cart. We can’t build this with Set since it doesn’t allow duplicate items.
  3. To-do list – Creating a to-do application requires using a List as you have to store the tasks list in order of their priority. You can’t use Set here since it doesn’t allow you to maintain the order.

Hope you have got the gist of this topic. Now let’s figure out different ways to convert Python Set to List.

Convert Python Set to List

Quick Summary

Overall, there are 6 main ways to convert a set to a list in python.

The choice of using these methods actually depends on the requirements you have.

  • If you want to create a sorted list from a set, then using the sorted() function is the efficient way.
  • Similarly, if you want to apply any transformations while converting to a list, you can opt for the list comprehension method.
  • Overall, extend() and unpacking solutions are efficient in terms of time.

We have compiled these 6 methods to calculate the time and space it takes to convert a set of 10 million numbers to list.

MethodTime Taken in MillisecondsMemory Usage In MB
extend() method87.54876800230704NA
Unpacking87.75791400694288NA
list() constructor90.0095870019868NA
sorted() method159.71354299108498NA
List comprehension241.50759598705918NA
Loop and append() method438.45207898993976NA

1. Using list() method

Typecasting Set to List in Python can be simply done by using the list() method. We just need to pass the set to list() constructor and it returns a new list with all the elements of a set.

#program to convert set to list in python using list() method

#initiating a set
data_set = {'code', 'threads', 'dev'}

#pass set to list() constructor to create a new list
list = list(data_set)

print(list)
#output: ['threads', 'dev', 'code']

  • Time complexity: O(n)
  • Space complexity: O(n)

Overall this method is a more efficient and simple solution to convert the set to a list in python. However, if you want additional functionalities like converting the set to a sorted list, you need to go with the third method.

2. Loop through the set to create a list

Another easy and naive way to convert a set to a list is by iterating over the set and adding each element to an empty list.

Here is the code thread for this.

#initiate a set
data_set = {'apple', 'microsoft', 'amazon', 'tesla', 'meta'}
new_list = []

#iterate over the set and add each element to list using append method
for element in data_set:
	new_list.append(element)

print(new_list)
#output: ['meta', 'apple', 'tesla', 'amazon', 'microsoft']

  • Time complexity: O(n)
  • Space Complexity: O(n)

3. Using sorted() function

sorted() function in python takes any of the iterable objects as a parameter and returns a sorted list. So when we pass a set to a sorted() function, it not only creates a new list but also sorts the elements.

You can even specify the sorting order, whether it is ascending or descending using a reverse parameter that takes a Boolean value.

#initiate a set
data_set = {'apple', 'microsoft', 'amazon', 'tesla', 'meta'}

#passing the set to sorted() function
new_list = sorted(data_set)

print(new_list)
#output: ['amazon', 'apple', 'meta', 'microsoft', 'tesla']

  • The time complexity of this method is O(n * log n). The sorted() function uses efficient sorting algorithms like merge sort internally.
  • Space complexity: O(n)

4. With List comprehension

Another concise way to create a list from a set in python is to use the list comprehension concept. You can even apply transformations on the set with this method.

#initiate a set
data_set = {'apple', 'microsoft', 'amazon', 'tesla', 'meta'}

#for comprehension code
new_list = [element for element in data_set]

print(new_list)
#output: ['meta', 'apple', 'tesla', 'amazon', 'microsoft']

  • Time complexity: O(n)
  • Space Complexity: O(n)

5. Using extend() method

The extend() method takes the elements to be added as an argument and adds these elements at the end of the list. This method accepts any of the iterable types (such as lists or sets) as input.

We can use this extend() method to create set to list in Python with the below code.

#initiating an empty list
new_list = []

data_set = {'apple', 'microsoft', 'amazon', 'tesla', 'meta'}

#using extend() method
new_list = new_list.extend(data_set)

print(new_list)
#output: ['apple', 'microsoft', 'amazon', 'tesla', 'meta']

  • Time complexity: O(n)
  • Space complexity: O(n)

6. Unpacking set

Unpacking is a popular concept among Python developers. As the name suggests, unpacking is used to extract elements from a collection. Below is the code example of using unpacking to extract elements from a Set to create a list.

Here is the code block to convert set to list in python –

#python code to create a list with set using unpack

data_set = {'apple', 'microsoft', 'amazon', 'tesla', 'meta'}

new_list = [*data_set, ]

print(new_list)
#output:['meta', 'apple', 'tesla', 'amazon', 'microsoft']

How to add Set to existing List?

There are two ways to add the set to an existing list.

  1. append() method
  2. extend() method

If you want to add the set directly to the list, you can use the append method. Whereas in extend(), each element of the set will be added to the list. Here are the example code snippets.

Using append() method

companies_list = ['apple', 'microsoft', 'amazon', 'tesla', 'meta']
companies_set = {'google', 'samsung', 'amazon', 'cisco'}

# Adding set to the list
companies_list.append(companies_set)

print(my_list)  
#output: ['apple', 'microsoft', 'amazon', 'tesla', 'meta', {'google', 'samsung', 'amazon', 'cisco'}]

Using extend() method

companies_list = ['apple', 'microsoft', 'amazon', 'tesla', 'meta']
companies_set = {'google', 'samsung', 'amazon', 'cisco'}

# Adding set to the list
companies_list.extend(companies_set)

print(my_list)  
#output: ['apple', 'microsoft', 'amazon', 'tesla', 'meta', 'google', 'samsung', 'amazon', 'cisco']

Can we use tolist() method to convert set to list?

No, we can’t use tolist() to create a list from the set.

tolist() method is available in the numpy library. It is used to create a python list from numpy array. It works by creating a new list and adding each element of an array to it.

The resultant list will have the same data and order as of original numpy array.

Frequently Asked Questions

  • What is the fastest way to create a list from set?

    We have tried running all the 6 methods for 10 million numbers. As per our observation, following are the fastest to create list from set in Python.

    1. extend() method
    2. list() constructor
    3. unpacking the set solution

    Find the detailed comparison report here.

  • What are the different ways to convert a set to a list?

    There are multiple ways to create a set from a list.

    1. list() method
    2. sorted() method
    3. extend() method

    When to use which one is detailed in this blog post.

  • How to add set data to an existing list?

    You can use the extend() method to add set data at the end of the list. Syntax and parameters are explained with code examples in this article.

Hope we are able to help you convert set to list in Python. If you have any other queries regarding this topic, let us know in the comment box below.

Leave a Comment

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