Python List Slicing

To access a range of items in a list, you need to slice a list. One way to do this is to use the simple slicing operator :

With this operator you can specify where to start the slicing, where to end and specify the step.

Slicing a List

If L is a list, the expression L [ start : stop : step ] returns the portion of the list from index start to index stop, at a step size step.

Syntax

Python List Slicing - Syntax

Basic Example

Here is a basic example of list slicing.

Example: Slice from index 2 to 7

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[2:7])    # ['c', 'd', 'e', 'f', 'g']
Python List Slicing Illustration

Note that the item at index 7 'h' is not included.

Slice with Negative Indices

You can also specify negative indices while slicing a list.

Example: Slice from index -7 to -2

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[-7:-2])    # ['c', 'd', 'e', 'f', 'g']
Python List Slicing - Negative Indices

Slice with Positive & Negative Indices

You can specify both positive and negative indices at the same time.

Example: Slice from index 2 to -5

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[2:-5])    # ['c', 'd']

Specify Step of the Slicing

You can specify the step of the slicing using step parameter.

The step parameter is optional and by default 1.

Example: Returns every 2nd item between position 2 to 7

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[2:7:2])    # ['c', 'e', 'g']
Python List Slicing - Specifying Step Size

Negative Step Size

You can even specify a negative step size.

Example: Returns every 2nd item between position 6 to 1

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[6:1:-2])    # ['g', 'e', 'c']

Slice at Beginning & End

Omitting the start index starts the slice from the index 0.

Meaning, L[:stop] is equivalent to L[0:stop]

Example: Slice the first three items from the list

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[:3])    # ['a', 'b', 'c']

Whereas, omitting the stop index extends the slice to the end of the list.

Meaning, L[start:] is equivalent to L[start:len(L)]

Example: Slice the last three items from the list

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[6:])    # ['g', 'h', 'i']

Reverse a List

You can reverse a list by omitting both start and stop indices and specifying a step as -1.

Example: Reverse a list with slicing operator

L = ['a', 'b', 'c', 'd', 'e']
print(L[::-1])    # ['e', 'd', 'c', 'b', 'a']

Modify Multiple List Values

You can modify multiple list items at once with slice assignment.

This assignment replaces the specified slice of a list with the items of assigned iterable.

Example: Modify multiple list items using slice

L = ['a', 'b', 'c', 'd', 'e']
L[1:4] = [1, 2, 3]
print(L)    # ['a', 1, 2, 3, 'e']

Example: Replace multiple elements in place of a single element

L = ['a', 'b', 'c', 'd', 'e']
L[1:2] = [1, 2, 3]
print(L)    # ['a', 1, 2, 3, 'c', 'd', 'e']

Insert Multiple List Items

You can insert items into a list without replacing anything. Simply specify a zero-length slice.

Example: Insert multiple list items using slice

# Insert at the start
L = ['a', 'b', 'c']
L[:0] = [1, 2, 3]
print(L)    # [1, 2, 3, 'a', 'b', 'c']
# Insert at the end
L = ['a', 'b', 'c']
L[len(L):] = [1, 2, 3]
print(L)    # ['a', 'b', 'c', 1, 2, 3]

You can insert items into the middle of list by keeping both the start and stop indices of the slice same.

Example: Insert multiple list items in the middle

# Insert in middle
L = ['a', 'b', 'c']
L[1:1] = [1, 2, 3]
print(L)    # ['a', 1, 2, 3, 'b', 'c']

Delete Multiple List Items

You can delete multiple items out of the middle of a list by assigning the appropriate slice to an empty list.

You can also use the del statement with the same slice.

Example: Delete multiple list items using slice

# assign empty list to slice
L = ['a', 'b', 'c', 'd', 'e']
L[1:5] = []
print(L)    # ['a']
# with del keyword
L = ['a', 'b', 'c', 'd', 'e']
del L[1:5]
print(L)    # ['a']

Clone or Copy a List

When you execute new_List = old_List, you don’t actually have two lists. The assignment just copies the reference to the list, not the actual list. So, both new_List and old_List refer to the same list after the assignment.

You can use slicing operator to actually copy the list (also known as a shallow copy).

Example: Create a copy of a list using slice (shallow copy)

L1 = ['a', 'b', 'c', 'd', 'e']
L2 = L1[:]
print(L2)       # ['a', 'b', 'c', 'd', 'e']
print(L2 is L1)	# False