Python - Remove positional rows

PythonServer Side ProgrammingProgramming

When it is required to remove positional rows, a simple iteration and the ‘pop’ method is used.

Example

Below is a demonstration of the same

my_list = [[31, 42, 2], [1, 73, 29], [51, 3, 11], [0, 3, 51], [17, 3, 21], [1, 71, 10], [0, 81, 92]]

print("The list is :")
print(my_list)
my_index_list = [1, 2, 5]

for index in my_index_list[::-1]:
my_list.pop(index)

print("The output is :")
print(my_list)

Output

The list is :
[[31, 42, 2], [1, 73, 29], [51, 3, 11], [0, 3, 51], [17, 3, 21], [1, 71, 10], [0, 81, 92]]
The output is :
[[31, 42, 2], [0, 3, 51], [17, 3, 21], [0, 81, 92]]

Explanation

• A nested list is defined and is displayed on the console.

• Another list with integer values is defined.

• This list is iterated over and reversed.

• Every index is deleted using the ‘pop’ method.

• This is the output which is displayed on the console.

Published on 15-Sep-2021 12:22:58