Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Odd elements removal in List
Next article icon

Python – Remove elements at Indices in List

Last Updated : 07 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given List, remove all the elements present in the indices list in Python.

Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2, 4, 5]  Output : [5, 6, 7, 2, 10]  Explanation : 3, 6, and 1 has been removed.  Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2]  Output : [5, 6, 7, 8, 1, 2, 10]  Explanation : 3 has been removed.  

In this article, we will cover how to Remove items at a specific index from Python List, and cover different methods that are listed below:

  • Remove an item by index and get its value using pop()
  • Remove items by index or slice using del.
  • Remove items at a specific index using enumerate() + loop 
  • Remove items at a specific index using enumerate() + list comprehension

Method 1: Remove an item by index and get its value using pop()

In this example, we will use the pop method to remove the element from the list, here in the pop we will pass the index value to remove the element at that position.

Python3

# initializing list
test_list = [5, 6, 3, 7, 8, 1, 2, 10]
 
test_list.pop(1)
print(test_list)
                      
                       

Output:

[5, 3, 7, 8, 1, 2, 10]

Time complexity: O(n), where n is the length of the list. 
Auxiliary space: O(1), since the operation does not require any additional space besides the list itself.

Method 2: Remove items by index or slice using del

In this example, we will use the del keyword to delete the specific elements present in the list. Here we will remove multiple items from the list by index. Please note that we need to sort the indices in reversed order to ensure that the shift of indices induced by the deletion of elements at lower indices won’t invalidate the index specifications of elements at larger indices.

Python3

test_list = [5, 6, 3, 7, 8, 1, 2, 10, 5]
indices=[3,7]
 
for i in sorted(indices, reverse=True):
    del test_list[i]
     
print(test_list)
                      
                       

Output:

[5, 6, 3, 8, 1, 2, 5]

Time complexity: O(nlogn), where n is the length of the test_list. 
Auxiliary Space: O(1), constant extra space is required

Method 3: Remove items at a specific index using enumerate() + loop

In this, we iterate for all the elements, and if the index is present in the list, then that index element is omitted from the result list.

Python3

# Python3 code to demonstrate working of
# Remove elements at Indices in List
# Using loop
 
# initializing list
test_list = [5, 6, 3, 7, 8, 1, 2, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing idx list
idx_list = [2, 4, 5, 7]
 
res = []
for idx, ele in enumerate(test_list):
     
    # checking if element not present in index list
    if idx not in idx_list:
        res.append(ele)
 
# printing results
print("Filtered List after removal : " + str(res))
                      
                       

Output:

The original list is : [5, 6, 3, 7, 8, 1, 2, 10] Filtered List after removal : [5, 6, 7, 2]

Time complexity: O(n), where n is the length of the input list test_list
Auxiliary space: O(n), where n is the length of the input list test_list. 

Method 4: Remove items at a specific index using enumerate() + list comprehension

In this, we perform the task of iteration using list comprehension in a compact way, the rest of the methods are similar to the above.

Python3

# Python3 code to demonstrate working of
# Remove elements at Indices in List
# Using enumerate() + list comprehension
 
# initializing list
test_list = [5, 6, 3, 7, 8, 1, 2, 10]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing idx list
idx_list = [2, 4, 5, 7]
 
# one-liner to test for element in index list
res = [ele for idx, ele in enumerate(test_list) if idx not in idx_list]
 
# printing results
print("Filtered List after removal : " + str(res))
                      
                       

Output:

The original list is : [5, 6, 3, 7, 8, 1, 2, 10] Filtered List after removal : [5, 6, 7, 2]

Time Complexity: O(n), where n is the length of the original list test_list. 
Auxiliary Space: O(m), where m is the length of the index list idx_list. 

Method#5: Using Recursive method.

Algorithm:

1. If the input index list is empty, return the original list.
2. Extract the first index from the input index list and recursively process the rest of the list.
3. Remove the element at the current index from the result of the recursive call.
4. Return the updated list.

Python3

def remove_elements_at_indices(test_list, idx_list):
    # Base case: if index list is empty, return original list
    if not idx_list:
        return test_list
 
    # Recursive case: extract first index and recursively process the rest of the list
    first_idx = idx_list[0]
    rest_of_indices = idx_list[1:]
    sub_list = remove_elements_at_indices(test_list, rest_of_indices)
 
    # Remove element at current index
    sub_list.pop(first_idx)
 
    return sub_list
test_list = [5, 6, 3, 7, 8, 1, 2, 10]
idx_list = [2, 4, 5, 7]
print("The original list is : " + str(test_list))
 
res = remove_elements_at_indices(test_list, idx_list)
print("Filtered List after removal : " + str(res))
                      
                       

Output
The original list is : [5, 6, 3, 7, 8, 1, 2, 10] Filtered List after removal : [5, 6, 7, 2]

Time complexity: O(m*n)

The function removes elements from the list for each index in the input index list, so the time complexity is O(m*n), where m is the length of the index list and n is the length of the input list.

Auxiliary Space:  O(max(m, n))

The function uses a recursive approach, so the space complexity is O(m), where m is the length of the input index list due to the recursive call stack. However, the space complexity of the list created is also O(n) as each element is removed one by one. So, the overall space complexity is O(max(m, n)).

 Method #6: Using filter() and lambda function:

Approach:

Create a new list using filter() function and lambda function.
The lambda function takes each element in test_list, and checks if its index is not in idx_list. If the index is not in idx_list, the lambda function returns True and the element is kept in the new list. If the index is in idx_list, the lambda function returns False and the element is filtered out.
The filtered new list is assigned to new_list.
The new_list is printed as the output.

Python3

test_list = [5, 6, 3, 7, 8, 1, 2, 10]
idx_list = [2, 4, 5]
new_list = list(filter(lambda x: test_list.index(x) not in idx_list, test_list))
print(new_list) # Output: [5, 6, 7, 2, 10]
                      
                       

Output
[5, 6, 7, 2, 10]

Time complexity: O(n^2) – We use index() method inside lambda function that has O(n) time complexity.
Auxiliary Space: O(n) – We create a new list to store the remaining elements.

METHOD 7: Using List Comprehension

APPROACH:

1.Create a new list by iterating through the original list and adding elements to the new list only if the index is not in the index list.
2.Return the updated list.

ALGORITHM:

1.Initialize a new list.
2.Iterate through the original list using a for loop.
3.Check if the index of the current element is present in the index list using an if condition.
4.If the index is not present in the index list, add the element to the new list.
5.Return the updated list.

Python3

test_list = [5, 6, 3, 7, 8, 1, 2, 10]
idx_list = [2, 4, 5]
 
new_list = [test_list[i] for i in range(len(test_list)) if i not in idx_list]
 
print("Updated list:", new_list)
                      
                       

Output
Updated list: [5, 6, 7, 2, 10]

Time Complexity: O(n)
Auxiliary Space: O(n)



Next Article
Python - Odd elements removal in List
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Remove an Element from a List by Index in Python
    We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2
    3 min read
  • Python - Indices of atmost K elements in list
    Many times we might have problem in which we need to find indices rather than the actual numbers and more often, the result is conditioned. First approach coming to mind can be a simple index function and get indices less than or equal than particular number, but this approach fails in case of dupli
    7 min read
  • Python - Duplicate Element Indices in List
    We are having a list we need to find the duplicate element indices. For example, we are given a list a = [10, 20, 30, 20, 40, 30, 50] we need to find indices of the duplicate items so that output should be {20: [1, 3], 30: [2, 5]}. Using a loop and a dictionaryWe iterate through the list using enume
    3 min read
  • Remove last K elements of list - Python
    Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Let’s explore different methods to remove the last K elements from a list in Python. Using list slicingList slicing is one of
    3 min read
  • Python - Odd elements removal in List
    Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
    5 min read
  • Remove Negative Elements in List-Python
    The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive num
    3 min read
  • Python | Remove given element from list of lists
    The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
    6 min read
  • Python - Remove non-increasing elements
    Given a list, our task is to write a Python program to remove all the non-increasing elements from the list. Input : test_list = [5, 3, 4, 5, 7, 3, 9, 10, 3, 10, 12, 13, 3, 16, 1] Output : [5, 5, 5, 7, 9, 10, 10, 12, 13, 16] Explanation : 3, 4 are omitted as 5, (greater element) had occurred before
    3 min read
  • Remove common elements from two list in Python
    When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. [GFGTABS] Python a = [1, 2, 3, 4, 5
    3 min read
  • Python | Remove given element from the list
    Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences