

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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.
Example
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)
Output
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]
Explanation
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.
- Related Questions & Answers
- Python Program to repeat elements at custom indices
- Python Group elements at same indices in a multi-list
- Python program to remove Duplicates elements from a List?
- Python Program to Remove Palindromic Elements from a List
- Find elements of a list by indices in Python
- Program to get indices of a list after deleting elements in ascending order in Python
- Python Program that print elements common at specified index of list elements
- Pair of similar elements at different indices in JavaScript
- Python program to remove duplicate elements from a Circular Linked List
- Python program to remove duplicate elements from a Doubly Linked List\n
- Python Program to get indices of sign change in a list
- C# program to remove duplicate elements from a List
- Java program to remove duplicates elements from a List
- Python program to get the indices of each element of one list in another list
- Program to find minimum possible difference of indices of adjacent elements in Python
Advertisements