Lest say you want the last element of a python list: what is the difference between
myList[-1:][0]
and
myList[len(myList)-1]
I thought there was no difference but then I tried this
>>> list = [0] >>> list[-1:][0] 0 >>> list[-1:][0] += 1 >>> list [0] >>> list[len(list)-1] += 1 >>> list [1]
I was a little surprised…
if you use slicing [-1:], the returned list is a shallow-copy, not reference. so [-1:][0] modifies the new list. [len(list)-1] is reference to last object.