Skip to content

Instantly share code, notes, and snippets.

@bolerap
Created November 3, 2016 04:24
Show Gist options
  • Save bolerap/736739edb57a3deba50209e19706d96a to your computer and use it in GitHub Desktop.
Save bolerap/736739edb57a3deba50209e19706d96a to your computer and use it in GitHub Desktop.
a = [1, 2, 3, 4, 5]
# slice [start:end]
print(a[1:3]) # [2 3]
print(a[-3: -1]) # [3, 4]
# slice [start:end:step]
print(a[::2]) # [1 3 5]
print(a[::-1]) # [5 4 3 2 1]
# slice assignment
a[1:-1] = []
print(a) # [1, 5]
# naming slice
b = list(range(5))
last_two = a[-2:]
print(b[last_two]) # [4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment