Python sorted() function space comlexity [duplicate] - python

This question already has answers here:
What is the space complexity of the python sort?
(2 answers)
Closed 2 years ago.
I know that sort() is O(1) space since the sorting is in place. However, sorted() function returns a new list with sorted elements. Since sorted() returns a new array with sorted elements, does this mean that an algorithm that uses the sorted() function takes O(n) space to execute?
Would sorted() be the same thing as creating an array and copying all elements, then running sort() on that new array?
for i in sorted(some_list):
// do something

sorted() uses Timsort: https://en.wikipedia.org/wiki/Timsort, which has O(n) space complexity.

Related

Python reverse() vs [::-1] slice performance [duplicate]

This question already has answers here:
Difference between reverse and [::-1]
(2 answers)
Time complexity of reversed() in Python 3
(1 answer)
Closed last month.
Python provides two ways to reverse a list:
List slicing notation
['a','b','c'][::-1] # ['c','b','a']
Built-in reversed() function
reversed(['a','b','c']) # ['c','b','a']
Are there any relevant differences in implementation/performance, or scenarios when one is preferred over the other?
The slicing operator constructs a new list in memory, which has implications on both memory and runtime.
reversed, on the other hand, returns an iterator which simply yields items one after another from the original list in reversed order. It does not allocate any additional memory.
So if all you're doing is iterating over the list in reverse and there's no need to construct a new (for example if you need to mutate it), then I'd say go for reversed.

Time complexity of converting tuple into a list (vice versa) [duplicate]

This question already has answers here:
Time complexity of casting lists to tuples in python and vice versa
(2 answers)
Closed 1 year ago.
if we are have a list lst = [1,2,3,4] and we convert it to a tuple like this tup = tuple(lst), what will be the time complexity of this code?
It is an O(N) operation, tuple(list) simply copies the objects from the list to the tuple.
so, you can still modify the internal objects(if they are mutable) but you can't add new items to the tuple
The time complexity is O(n) because the underlying operation is simply taking the original numbers and copying the objects into a tuple rather than a list.
Python list time complexity: https://wiki.python.org/moin/TimeComplexity
Operation
Average Case
Amortized Worst Case
Copy
O(n)
O(n)

Time complexity of in function in python [duplicate]

This question already has answers here:
Complexity of *in* operator in Python
(3 answers)
Closed 1 year ago.
I would like to check whether x in list(y), hence I use the code
if x in y:
return True
what is the time complexity? is it O(1) or O(n)?
Thank you
It depends on what type of object y is.
If y is a sequence type like list or tuple, the time complexity is O(n), because Python has to scan the sequence looking for a match.
If y is a hashed type like a set or dict, the time complexity is typically O(1), because Python can immediately check whether a matching object exists in the hash table.
Update: the question was edited to indicate that y is a list. In that case, the time complexity is O(n).
Also see this duplicate question and more background info.

Itertools Combinations/Permutations size [duplicate]

This question already has answers here:
Is there any built-in way to get the length of an iterable in python?
(10 answers)
What's the shortest way to count the number of items in a generator/iterator?
(7 answers)
Closed 4 years ago.
Is there anyway to see the len() of an itertools.Combination or other object, really, without materializing it to a list?
I can get the cardinality of combs or permutations with the factorials,... but I want something that generalizes.
Thanks
For any iterable it, you can do:
length = sum(1 for ignore in it)
That doesn't create a list, so the memory footprint is small. But for many kinds of iterables, it also consumes it (for example, if it is a generator, it's consumed and can't be restarted; if it is a list, it's not consumed). There is no generally "non-destructive" way to determine the length of an arbitrary iterable.
Also note that the code above will run "forever" if it delivers an unbounded sequence of objects.
No need to create a list. You can count the number of items in an iterable without storing the entire set:
sum(1 for _ in myIterable)
Yes,
def count_iterable(i):
return sum(1 for e in i)
Taken from: Is there any built-in way to get the length of an iterable in python?

lower versus length syntax in python? [duplicate]

This question already has answers here:
Why does Python code use len() function instead of a length method?
(7 answers)
Closed 7 years ago.
I'm new to Python and I have a question about the string operations. Is there an over-arching reason that I should understand as to why the lower operation is written as 'variable.lower()' while another one, say length, would be written as 'len(variable)'?
lower is a string method, that is, a function built in to the string object itself. It only applies to string objects.
len is a built in function, that is, a function available in the top namespace. It can be called on many different objects (strings, lists, dicts) and isn't unique to strings.

Categories