Python | Program to remove all elements in a range from the List

Here, we are going to learn how to remove all elements in a range from the list in Python? To remove all elements in a range from the list we use del() method of Python. By IncludeHelp Last updated : June 22, 2023

Given a list and we have to remove elements in a range from the list in Python.

Remove all elements in a range from the Python list

To remove all elements in a range from the Python list, you can simply use the del keyword with the list by passing the range i.e., from the start_index to end_index.

Syntax

The syntax for deleting elements in a range from the Python list is as follows:

del list(start_index : end_index)

Program

In the below program, we have a list with integer elements, and we're deleting the elements from a range between index 1 to index 3 using the del keyword.

# Declaring a list
list1 = [10, 20, 30, 40, 50]

# print the list
print("Original list:", list1)

# delete elements from index 1 to 3
del list1[1:3]

# print list after deleting
# elements from index 1 to 3
print("Updated list after deleting the elements:")
print(list1)

Output

Original list: [10, 20, 30, 40, 50]
Updated list after deleting the elements:
[10, 40, 50]

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.