- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to remove elements at Indices in List
When it is required to remove the elements at indices in a list, the ‘enumerate’ attribute, the ‘not in’ operator, a simple iteration and the ‘append’ methods are used.
Below is a demonstration of the same −
my_list = [91, 75, 15, 45, 69, 78, 23, 71, 36, 72] print("The list is : " ) print(my_list) print("The list after sorting is : " ) my_list.sort() print(my_list) index_list = [2, 4, 5, 7] print("The index values stored in the list are :") print(index_list) my_result =  for index, element in enumerate(my_list): if index not in index_list: my_result.append(element) print("The resultant list is : ") print(my_result) print("The list after sorting is : " ) my_result.sort() print(my_result)
The list is : [91, 75, 15, 45, 69, 78, 23, 71, 36, 72] The list after sorting is : [15, 23, 36, 45, 69, 71, 72, 75, 78, 91] The index values stored in the list are : [2, 4, 5, 7] The resultant list is : [15, 23, 45, 72, 78, 91] The list after sorting is : [15, 23, 45, 72, 78, 91]
A list is defined and is displayed on the console.
It is sorted and is displayed on the console.
The index values are stored in a list.
They are also displayed on the console.
An empty list is created.
The list is iterated over, and an ‘if’ condition is placed.
This checks to see if the index is not present in the index value list.
If not, the element is append to the empty list.
This is displayed as the output on the console.
The list is sorted again and displayed on the console.
Kickstart Your Career
Get certified by completing the courseGet Started