0

hope you are all doing fine. I just want to know what is runtime complexity for this case? and is the run time complexity for all cases such as dictionary, list, tuple and etc... to extract of the end of these things with [-1] the same? i had searched in many pages but i didn't find it.

list = [1,2,3]
print(list[-1])
>>> 3
  • Unfortunately the question in this form doesn't make sense: It depends, on how the implementation of `__getitem__` actually looks like, which is more or less structure-dependent. It is `O(1)` for `dict`s and `O(n)`, n the length, for `list`s (see [here](https://stackoverflow.com/a/513906/14311263)), but it can be anything for another class which implements it. – Timus Nov 03 '21 at 12:28
  • tnx, i fixed it – amirhossein Nov 04 '21 at 08:40
  • If you [search for `lookup complexity` under the `python` tag](https://stackoverflow.com/search?q=%5Bpython%5D+lookup+complexity) you'll find a lot of questions and answers related to your question. – Timus Nov 04 '21 at 10:18

0 Answers0