-1

I know that the last element of a list say li can be accessed by li[-1] but how exactly does it work in the background? Is it same as li[len(li) - 1]

Is there any other way of getting the last element of a list without actually knowing the length of the list? I am looking at this question in terms of efficiency so please suggest any other alternate solutions with less complexity if it exists.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Nagarjun Prasad
  • 802
  • 3
  • 17
  • 31
  • 1
    Yet even *more* efficient than `li[-1]`? In terms of speed? Complexity? Number of keystrokes? – Jongware Dec 04 '18 at 19:05
  • 2
    There is no efficiency concern to be had. Just use `[-1]` – roganjosh Dec 04 '18 at 19:05
  • Although now I'm thinking... lists allocate extra space to expand so maybe it isn't so simple as to dismiss the question off-hand? It would only ever be a micro-optimization at best, but still not so clear how it actually works. Presumably a memory allocation has nothing to do with the indexing itself. – roganjosh Dec 04 '18 at 19:06
  • @usr2564301 Hmm I have mentioned in the question that I am looking for anything with less "complexity if it exists" what is this number of keystrokes saved? – Nagarjun Prasad Dec 04 '18 at 19:09
  • They are suggesting that there may be another method that would make you more efficient by having fewer keys to press to write the code. It really isn't an efficiency concern in terms of speed, we're talking really, really small time differences if they exist at all – roganjosh Dec 04 '18 at 19:12
  • 1
    Possible duplicate of [Getting the last element of a list in Python](https://stackoverflow.com/questions/930397/getting-the-last-element-of-a-list-in-python) "`some_list[-1]` is the shortest and most Pythonic." – chickity china chinese chicken Dec 04 '18 at 19:12

3 Answers3

1

You probably want to read this:

https://docs.python.org/2/faq/design.html#how-are-lists-implemented-in-cpython

It said,

CPython’s lists are really variable-length arrays, not Lisp-style linked lists. The implementation uses a contiguous array of references to other objects, and keeps a pointer to this array and the array’s length in a list head structure.

That's why li[-1] is recommended and most efficient.

adrtam
  • 6,991
  • 2
  • 12
  • 27
0

li[-1] is the fastest and more Pythonic way to do it.

You can do li[-n] to get the nth element starting from the end, it is just an access by index and it is in fact faster than li[len(li) - 1].

If you have a list in python the length is given so li[1] works the same way as li[-1].

Albondi
  • 1,141
  • 1
  • 8
  • 19
0

From docs

The formal syntax makes no special provision for negative indices in sequences; however, built-in sequences all provide a getitem() method that interprets negative indices by adding the length of the sequence to the index (so that x[-1] selects the last item of x).

So yes, internally it DOES work as -1 + len(li)

However, here is the kicker. len is a constant time/O(1) operation, and does not depend on the length of the list. So, negative indexing is really efficient, and really IS the right way you should be accessing elements. That is why it is there after all.

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33