Grammar/Environments List

[Python]How to Slice a List

This article shows how to slice a list.

Contents

  • Basic usage of slicing
  • How to slice from the end with a negative index
  • How to remove elements by using slicing
  • How to remove an element at the end of a list by using slicing
  • How to insert elements by using slicing
  • How to slice a 2D list

sponsored link

Basic usage of slicing

In the beginning, I show basic usage of slicing.

In order to access a range of elements in a list, you need to slice a list.

To use list slicing, specify the start position, end position, and number of steps, separated by slicing operator i.e. colon(:).

The step parameter is optional and by default 1.

How to write

List[ Start : End : Step ]

List[ Start : End ]

This is an example code for slicing a 1D list.

my_list = list(range(0,10))
print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(my_list[2::])
# [2, 3, 4, 5, 6, 7, 8, 9]
print(my_list[2:])
# [2, 3, 4, 5, 6, 7, 8, 9]

print(my_list[:2:])
# [0, 1]
print(my_list[:2])
# [0, 1]

print(my_list[2:5:])
# [2, 3, 4]
print(my_list[2:5])
# [2, 3, 4]

This is an example code in which the number of steps is specified.

my_list = list(range(0,10))
print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(my_list[2:7:])
# [2, 3, 4, 5, 6]
print(my_list[2:7:2])
# [2, 4, 6]

How to slice from the end with a negative index

By specifying negative indices for the number of steps, the elements of a list can be sliced in reverse order.

This is an example code.

my_list = list(range(0,10))
print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(my_list[::-1])
# [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(my_list[5:1:-1])
# [5, 4, 3, 2]
print(my_list[1:5:-1])
# []

If the start position is less than the end position, a sliced list is empty.

sponsored link

How to remove elements by using slicing

I show how to remove elements by using slicing.

I show two example codes.

Elements can be removed by specifying a sliced list in "del statement".

my_list = list(range(0,10))
print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

del my_list[2:5]

print(my_list)
# [0, 1, 5, 6, 7, 8, 9]

Elements can also be removed by using an empty list instead of "del statement".

my_list = list(range(0,10))
print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

my_list[2:5] = []

print(my_list)
# [0, 1, 5, 6, 7, 8, 9]

How to remove elements is explained in this article.

How to Remove Elements from List [del, remove, pop, clear]

How to remove an element at the end of a list by using slicing

You can remove the element at the end of a list by specifying "-1" at the end position.

This is an example code.

my_list = list(range(0,10))
print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(my_list[:-1:])
# [0, 1, 2, 3, 4, 5, 6, 7, 8]
print(my_list[:-1])
# [0, 1, 2, 3, 4, 5, 6, 7, 8]

sponsored link

How to insert elements by using slicing

I show how to insert elements by using slicing.

By using list slicing, specify the position at which elements are to be inserted.

This is an example code for replacing elements of a list.

my_list = list(range(0,10))
print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

my_list[2:5] = [20, 30, 40]

print(my_list)
# [0, 1, 20, 30, 40, 5, 6, 7, 8, 9]

This is an example code for inserting elements into a list.

my_list = list(range(0,10))
print(my_list)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

my_list[2:2] = [20, 30, 40]

print(my_list)
# [0, 1, 20, 30, 40, 2, 3, 4, 5, 6, 7, 8, 9]

How to slice a 2D list

I show how to slice a 2D list.

This is an example code.

list_2d = [
    [11, 12, 13, 14],
    [21, 22, 23, 24],
    [31, 32, 33, 34],
    [41, 42, 43, 44],
    [51, 52, 53, 54]
]

print(list_2d[2:4])
# [[31, 32, 33, 34], [41, 42, 43, 44]]

An example code for slicing after specifying a 1D list.

list_2d = [
     [11, 12, 13, 14],
     [21, 22, 23, 24],
     [31, 32, 33, 34],
     [41, 42, 43, 44],
     [51, 52, 53, 54]
]

print(list_2d[1][1:3])
# [22, 23]

This is an example code for using list comprehensions and slicing.

list_2d = [
    [11, 12, 13, 14],
    [21, 22, 23, 24],
    [31, 32, 33, 34],
    [41, 42, 43, 44],
    [51, 52, 53, 54]
]

print([row[1:3] for row in list_2d[2:4]])
# [[32, 33], [42, 43]]

How to use comprehension is explained in this article.

How to use comprehension

sponsored link

-Grammar/Environments, List
-,