Python List Slicing Using Colon
Python List Slicing Using Colon
Published
- Items start through stop-1
- Items start through the rest of the List
- Items from the beginning through stop-1
- Copy of the Complete List
- Last item in the List
- Last two items in the List
- Everything except the last two items
- All items in the List, in reversed order
- The first two items, reversed
- The last two items, reversed
- Everything except the last two items, reversed
Python List Slicing Using Colon
Python list slicing using colon(:) operator is a way to access elements or a range of elements in a list. Using colon operator, we can specify where to start the slicing, where to end, and specify the step.
Python List slicing always returns a new list or empty list from the existing list.
Python slicing for list and string are similar. So, the operations we can perform on list is similar for string.
Syntax
Lst[ StartIndex : StopIndex : StepIndex ]
Items Start through Stop-1
In Python slicing after brackets we can provide start and stop value like python list1[start:stop]
to get the items from list or string from start to stop minus one position.
Please note in Python list and string always start from zero position.
# Syntax: list1[start:stop]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[0:5])
# Result: [1, 2, 3, 4, 5]
Items start through the Rest of the List
In Python slicing after brackets we can just provide start and skip stop value like python list1[start:]
to get all the items from list from starting position to the end of list.
Please note after start Index we need to add colon.
# Syntax: list1[start:]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[5:])
# Result: [6, 7, 8, 9, 10]
Items from the Beginning through Stop-1
In Python slicing after brackets we can just skip startIndex and after colon just provide the stopIndex value like python list1[:stop]
to get the items from start of list to the stopIndex position of list.
# Syntax: list1[:stop]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[0:5])
# Result: [1, 2, 3, 4]
Copy of the Complete List
In Python slicing after brackets we can skip both startIndex and stopIndex to get a copy of list.
# Syntax: list1[:]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[:])
# Result: [1,2,3,4,5,6,7,8,9,10]
Last Item in the List
To get the last item in a Python list use negative itemIndex like python list1[-1]
will return last IndexItem.
# Syntax: a[-1]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[-1])
# Result: 10
Last two items in the List
To get the last two items from a Python list use negative -2 in startIndex and skip the stopIndex like python list1[-2:]
will return last two IndexItem values.
# Syntax: a[-2:]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[-2:])
# Result: [9, 10]
Everything Except the Last Two Items
To get the everything except the last two items from a Python list skip startIndex value and after colon provide -2 as stopIndex value like python list1[-2:]
will return everything except the last two items.
# Syntax: a[:-2]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[:-2])
# Result: [1, 2, 3, 4, 5, 6, 7, 8]
All items in the List, in Reversed Order
To get All items in the list in reversed order. Use following code.
# Syntax: a[::-1]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[::-1])
# Result: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
The First Two Items, Reversed
To get the first two items the list in reversed order. Use the following code.
# Syntax: a[1::-1]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[1::-1])
# Result: [2, 1]
The Last Two Items, Reversed
To get the last two items in the list in reversed order. Use the following code.
# Syntax: a[:-3:-1]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[:-3:-1])
# Result: [10, 9]
Everything Except the Last Two Items, Reversed
To get the everything in the list except the last two items, reversed. Use the following code.
# Syntax: a[-3::-1]
list1 = [1,2,3,4,5,6,7,8,9,10]
print (list1[-3::-1])
# Result: [8, 7, 6, 5, 4, 3, 2, 1]