Yes. Iterating over a list is O(n) and adding each element to the hash set is O(1), so the total operation is O(n).

Answer from Mad Physicist on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ time-complexity-of-a-list-to-set-conversion-in-python
Time Complexity of A List to Set Conversion in Python - GeeksforGeeks
July 23, 2025 - The time to convert small list to set : 0.0 The set is : {1, 2, 3, 4, 5} The time to convert large list to set : 0.21737 ยท The time complexity of list to set conversion is O(n) where n is the number of element in the list.
Discussions

Running time for converting list to set in Python - Stack Overflow
In Python, what are the running time and space complexities if a list is converted to a set? Example: data = [1,2,3,4,5,5,5,5,6] # this turns list to set and overwrites the list data = set(data) ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Algorithmic complexity to convert a set to a list in python - Stack Overflow
In python, when I convert my set to a list, what is the algorithmic complexity of such a task? Is it merely type-casting the collection, or does it need to copy items into a different data structure? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Why is converting a list to a set faster than using just list to compute a list difference? - Stack Overflow
Say, I wish to compute the difference of two lists C = A - B: A = [1,2,3,4,5,6,7,8,9] B = [1,3,5,8,9] C = [2,4,6,7] #Result A and B are both sorted with unique integers (not sure if ther... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Time complexity for function converting list to set and vice versa - Stack Overflow
1 How are lists converted into sets in Python 3? And what's the performance/complexity of this conversion? More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 19, 2021
People also ask

What is the average time complexity for converting a list to a set in Python?
A: The average time complexity for converting a list to a set is O(n), where n is the number of elements in the list.
๐ŸŒ
sqlpey.com
sqlpey.com โ€บ python โ€บ how-to-analyze-time-complexity-of-list-to-set-conversion-in-python
How to Analyze the Time Complexity of List to Set Conversion in Python
What causes the time complexity to become O(n^2) when converting a list to a set?
A: The time complexity can become O(n^2) in the worst-case scenario due to hash collisions that necessitate additional handling, leading to inefficient element insertion.
๐ŸŒ
sqlpey.com
sqlpey.com โ€บ python โ€บ how-to-analyze-time-complexity-of-list-to-set-conversion-in-python
How to Analyze the Time Complexity of List to Set Conversion in Python
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ convert list to set python
Convert List to Set Python - Scaler Topics
January 16, 2024 - 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. ... 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).
Top answer
1 of 3
18

There is overhead to convert a list to a set, but a set is substantially faster than a list for those in tests.

You can instantly see if item x is in set y because there's a hash table being used underneath. No matter how large your set is, the lookup time is the same (basically instantaneous) - this is known in Big-O notation as O(1). For a list, you have to individually check every element to see if item x is in list z. As your list grows, the check will take longer - this is O(n), meaning the length of the operation is directly tied to how long the list is.

That increased speed can offset the set creation overhead, which is how your set check ends up being faster.

EDIT: to answer that other question, Python has no way of determining that your list is sorted - not if you're using a standard list object, anyway. So it can't achieve O(log n) performance with a list comprehension. If you wanted to write your own binary search method which assumes the list is sorted, you can certainly do so, but O(1) beats O(log n) any day.

EDIT 2:

I'm aware that a set's average O(1) lookup time beats that of a list's O(n) but if the original list A contains about a million or so integers, wouldn't the set creation actually take longer?

No, not at all. Creating a set out of a list is a O(n) operation, as inserting an item into a set is O(1) and you're doing that n times. If you have a list with a million integers in it, converting it into a set involves two O(n) steps, while repeatedly scanning the list is going to be n O(n) steps. In practice, creating the set is going to be about 250,000 times faster for a list with a million integers, and the speed difference will grow larger and larger the more items you have in your list.

2 of 3
9

According to the Python documentation on time complexity

  • List membership x in s is on average linear-time operation, or O(n).
  • Set membership x in s is on average constant-time operation, or O(1).

Building a set is worst-case linear-time operation, because one would need to scan all the elements in a list to build a hash-table, so O(n). n is number of elements in a collection.

The key observation is that, in Method 1, building a set, s = set(B) is just a one-off operation, then after that we just have n total number of set-membership test as in x not in B, so in total O(n) + n * O(1), or O(n) time complexity.

Whereas in Method 2, the list-membership test x not in B is carried out for each element in A, so in total n * O(n) = O(n^2) time complexity.

๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python list to set conversion [interactive guide]
Python List to Set Conversion [Interactive Guide] - Be on the Right Side of Change
May 24, 2020 - ... Exercise: Add another string 'Alice' to the list friends and see the resulting set. How many elements do the list and the set have? The time complexity of converting a list to a set is linear in the number of list elements.
Find elsewhere
๐ŸŒ
sqlpey
sqlpey.com โ€บ python โ€บ how-to-analyze-time-complexity-of-list-to-set-conversion-in-python
How to Analyze the Time Complexity of List to Set Conversion in Python
November 24, 2024 - A: The average time complexity for converting a list to a set is O(n), where n is the number of elements in the list. A: The time complexity can become O(n^2) in the worst-case scenario due to hash collisions that necessitate additional handling, leading to inefficient element insertion.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Understanding Time Complexity for Converting List to Set and Vice Versa - YouTube
Explore the time complexity of converting lists to sets and back. Learn the efficient approaches for implementing these conversions in Python.---This video i...
Published: May 26, 2025
Views: 0
๐ŸŒ
TraceDynamics
tracedynamics.com โ€บ python-list-to-set
Python List to Set: Conversion Methods and Performance Insights
December 7, 2023 - Simplicity: The set() constructor provides a simple and readable way to convert a list to a set. Performance: Itโ€™s the fastest method for conversion with a time complexity of O(N), where N is the number of elements in the list
๐ŸŒ
YouTube
youtube.com โ€บ hey delphi
PYTHON : What is time complexity of a list to set conversion? - YouTube
PYTHON : What is time complexity of a list to set conversion?To Access My Live Chat Page, On Google, Search for "hows tech developer connect"As promised, I h...
Published: April 20, 2023
Views: 9
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ lookup time in a list vs. set
r/Python on Reddit: Lookup time in a list vs. set
October 12, 2020 - But you have to account for the time it takes to convert a list to a set, and that takes "ages" - 187 nsec for a list of one element, 15.3 usec for 1000 element, 27.7 msec for 1 000 000 elements, and so on.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ time-complexity-to-convert-a-set-to-list-in-python
Time Complexity to Convert a Set to List in Python - GeeksforGeeks
July 23, 2025 - List is : [1, 2, 3, 4, 5] Small Set to List Time in sec: 0.0 Large Set to List Time in sec: 0.011127 ยท List comprehension is the short way for doing a task. And we can also use list comprehension for conversion of list to set.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 48393707 โ€บ complexity-of-building-a-set-from-list โ€บ 48393750
python - Complexity of building a set from list? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... Closed 8 years ago. What is the computational complexity of the following operation: ... Save this answer. ... Show activity on this post. It is O(n), where n is the number of elements in the list. set() is essentially iterating over the list and adding each of those elements to a hash table.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ list-to-set-python
Convert List to Set in Python | 4 Methods (With Example)
September 15, 2023 - In Python, we have four different ways to transform a list into a set. Let's take a closer look at each method step by step. The simplest and most straightforward method to convert a list to a set in Python is by using the set() function. The set() function takes an iterable, such as a list, as an argument and returns a set containing all the unique elements from the iterable.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ time-complexity-to-convert-a-set-to-list-in-python
Time Complexity to Convert a Set to List in Python - GeeksforGeeks
July 23, 2025 - Let's denote the size of the set as n. The time complexity to convert a set to a list generally takes O(n) time. where n is the n of elements in the set. There are various methods to convert a set to a list in Python.
๐ŸŒ
Aloneguid
aloneguid.uk โ€บ posts โ€บ 2024 โ€บ 02 โ€บ list-set-dic-complexity-python
List, set, and dictionary complexity in Python - Aloneguid
February 7, 2024 - You could go through each person one by one (like a list), or you could ask everyone to form groups based on their first name (like a set). Which do you think would be faster? Letโ€™s find out! 1# We have a party with 100,000 guests 2guest_list = list(range(100000)) 3guest_set = set(guest_list) 4 5%timeit 50000 in a_list_range 6%timeit 50000 in a_set_range 7%timeit 500000 in a_list_range 8%timeit 500000 in a_set_range
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ time-complexity-for-adding-element-in-python-set-vs-list
Time Complexity for Adding Element in Python Set vs List - GeeksforGeeks
December 19, 2024 - The average-case time complexity is O(n), where n is the number of elements in the list. In this article, we will see compare different scenarios in which we ... Given a set and list, the task is to write a python program to check if any set ...