Skip to content

Instantly share code, notes, and snippets.

@mirekfranc
Last active October 10, 2016 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirekfranc/61e40a183171b9c123f4f40a45e99693 to your computer and use it in GitHub Desktop.
Save mirekfranc/61e40a183171b9c123f4f40a45e99693 to your computer and use it in GitHub Desktop.
access to the last element of an array in python
>>> a = [1, 2, 3]
>>> import dis
>>> def f():
print a[len(a)-1]
... ...
>>> def g():
print a[-1]
... ...
>>> dis.dis(f)
2 0 LOAD_GLOBAL 0 (a)
3 LOAD_GLOBAL 1 (len)
6 LOAD_GLOBAL 0 (a)
9 CALL_FUNCTION 1
12 LOAD_CONST 1 (1)
15 BINARY_SUBTRACT
16 BINARY_SUBSCR
17 PRINT_ITEM
18 PRINT_NEWLINE
19 LOAD_CONST 0 (None)
22 RETURN_VALUE
>>> dis.dis(g)
2 0 LOAD_GLOBAL 0 (a)
3 LOAD_CONST 1 (-1)
6 BINARY_SUBSCR
7 PRINT_ITEM
8 PRINT_NEWLINE
9 LOAD_CONST 0 (None)
12 RETURN_VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment