Is O((b-a)/c) the time complexity of list[a:b:c] in python3?

Question:

According to https://wiki.python.org/moin/TimeComplexity, the complexity of "Get a Slice" from list is O(k) and k is the length of wanted slice.

If there is a step parameter, the complexity will become O(k/step)? (i.e. O((b-a)/c) for list[a:b:c])

Asked By: shenlebantongying

||

Answers:

Surely it is. Getting a value from a list via its index, is O(1). Getting k items from a list via their indexes, is O(k). No matter you are getting them with steps or getting them without steps or even without slices:

l = list(range(10))
l1 = l[1:6:2]
l2 = [l[1], l[3], l[5]]
l3 = [l[i] for i in range(1,6,2)]

They are all O(number of elements)

Answered By: Mohammad Jafari
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.