Holla Tech - Learn

List Slices
 

List slices provide a more advanced way of retrieving values from a list. Basic list slicing involves indexing a list with two colon-separated integers. This returns a new list containing all the values in the old list between the indices.
Example: 

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(squares[2:6])
print(squares[3:8])
print(squares[0:1]) 

 

Result: 

>>
[4, 9, 16, 25] [9, 16, 25, 36, 49] [0] >>> 

 

NOTE!
Like the arguments to range, the first index provided in a slice is included in the result, but the second isn’t.

List Slices
 

If the first number in a slice is omitted, it is taken to be the start of the list.
If the second number is omitted, it is taken to be the end.
Example:

Result: 

>>
[0, 1, 4, 9, 16, 25, 36] [49, 64, 81] >>> 

 

NOTE!
Like the arguments to range, the first index provided in a slice is included in the result, but the second isn’t.

List Slices
 

List slices can also have a third number, representing the step, to include only alternate values in the slice.

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(squares[::2])
print(squares[2:8:3]) 

 

Result: 

>>
[0, 4, 16, 36, 64] [4, 25] >>> 

 

NOTE!
[2:8:3] will include elements starting from the 2nd index up to the 8th with a step of 3.

List Slices
 

Negative values can be used in list slicing (and normal list indexing). When negative values are used for the first and second values in a slice (or a normal index), they count from the end of the list.

squares = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print(squares[1:-1]) 

 

Result: 

>>
[1, 4, 9, 16, 25, 36, 49, 64] >>> 

 

NOTE!
If a negative value is used for the step, the slice is done backwards. Using [::-1] as a slice is a common and idiomatic way to reverse a list.

BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE PYTHON MAIN COURSE PAGE. 

PYTHON MAIN COURSE PAGE

 


©️ License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT