Python Language List slicing (selecting parts of lists)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • a[start:end] # items start through end-1
  • a[start:] # items start through the rest of the array
  • a[:end] # items from the beginning through end-1
  • a[start:end:step] # start through not past end, by step
  • a[:] # a copy of the whole array
  • source

Remarks

  • lst[::-1] gives you a reversed copy of the list
  • start or end may be a negative number, which means it counts from the end of the array instead of the beginning. So:
a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

(source)



Got any Python Language Question?