# Last two items of ENTIRE list ls = [1, 2, 3, 4, 5, 6, 7, 8] print(ls[-2:]) # [7, 8] # equivalent to ls[len(ls)-2:len(ls)], see below # Last two items of A PART of list cutoff = 5 print(ls[cutoff-2:cutoff]) # where -2 is last 2 values # [4, 5] # Using a for loop for i in range(0, len(ls)): if i+1 == len(ls)): print(ls[i-1:len(ls)]) # [7, 8] # Negative numbers in slices are simply evaluated by adding `len(ls)`, so this is the same as `ls[len(ls) - 2:]`. For more information on slices, refer to the [Python tutorial](http://docs.python.org/tutorial/introduction.html#strings) or [this excellent stackoverflow answer](https://stackoverflow.com/a/509295/35070).
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter