Built with Tutify.io

    AIStudyLabs

    Python Remove from List by Index

    Python Remove from List by Index

    How to remove an element/item from a list by index in Python? To remove an element from a list by index use the list.remove(), pop(), enumerate(), List comprehension, and del keyword. In this article, I will explain by using all these methods with examples.

    <a id="h-1-quick-examples-of-remove-from-list-by-index"></a>

    1. Quick Examples of Remove from List by Index

    If you are in a hurry, below are some quick examples of how to remove items from the list by index.

    
    # Quick examples to remove item from list by index
    
    # Example 1: Using remove() by Index
    technology = ["Hadoop", "Spark", "Python","Java", "Pandas"]
    technology.remove(technology[1])
    
    # Example 2: Using pop() function
    numbers = [2, 5, 8, 4, 1, 7, 3, 9]
    numbers.pop(3)
    
    # Example 3: Using del keyword
    del numbers[i]
    
    # Example 3: Remove multiple elements by index 
    numbers = [2, 5, 8, 4, 1, 7, 3, 9]
    indixes=[2,4]
    for i in sorted(indixes, reverse=True):
        del numbers[i]
    
    # Example 4: Remove element by index 
    # Using enumerate() + loop
    numbers = [2, 5, 8, 4, 1, 7, 3, 9]
    indixes=[2,4,6]
    num = []
    for ind, ele in enumerate(numbers):
        if ind not in indixes:
            num.append(ele)
    
    # Example 5: Remove an element by index 
    # Using enumerate() + List Comprehension
    numbers = [2, 5, 8, 4, 1, 7, 3, 9, 14]
    indixes=[2, 4, 6, 8]
    num = [ele for idx, ele in enumerate(numbers) if idx not in indixes]
    

    2. Syntax of remove()

    Following is a syntax of the remove().

    
    # Using remove() with element
    mylist.remove(element)
    
    # Using remove() with index position
    mylist.remove(mylist[index])
    

    Here, mylist is the list object from where the element is removed.

    <a id="h-3-remove-list-item-by-index-in-python-using-remove"></a>

    3. Remove List Item by Index in Python using remove()

    To remove a list item/element by Index use the remove(list[index]) in Python, you can use this only if you know the index of the element you wanted to remove. Note that the remove() method doesn’t take the index as an argument however, you can get the element by index using list[index] and use the value to the method. Let's create a list named technology and remove the Spark element from it by using an index.

    
    # Consider the list of strings
    technology = ["Hadoop", "Spark", "Python","Java", "Pandas"]
    print("Actual List: ",technology)
    
    # Using remove() by Index
    technology.remove(technology[1])
    print("Final List: ",technology)
    

    This example yields the below output.

    Here it removed the element from the 1st index which is the 2nd element from the list (Index starts from 0).

    <a id="h-4-remove-element-from-the-list-by-index-using-pop"></a>

    4. Remove Element from the List by Index using pop()

    You can use the pop() method to remove an item/element from a list by its index, this takes the index as an argument and returns the removed item, so it can be stored in a variable if needed.

    
    # Use pop() to remove item from the list by index 
    numbers = [2, 5, 8, 4, 1, 7, 3, 9]
    numbers.pop(3)
    print(numbers)
    
    # Output:
    # [2, 5, 8, 1, 7, 3, 9]
    

    Here, it removed the element from the 3rd index which is the 4th element from the list.

    5. Using del Keyword

    Similarly, you can use the del keyword to delete the element by index.

    
    # Use del keyword 
    numbers = [2, 5, 8, 4, 1, 7, 3, 9]
    del numbers[3]
    print(numbers)
    
    # Output:
    # [2, 5, 8, 1, 7, 3, 9]
    

    <a id="h-6-remove-multiple-elements-from-list-by-index"></a>

    6. Remove Multiple Elements from List by Index

    To remove the multiple elements/items from the python list by index, you can use any above-mentioned methods, however, I will use the del keyword to explain. In order to use this you need a list of element indexes you wanted to remove and use it with for loop to remove iteratively.

    Here, the sorted() function sorts the list of indices in reverse order to ensure that the removal of elements at lower indices won't affect the index specifications of elements at larger indices.

    
    # Consider list
    technology = ["Hadoop", "Spark", "Python","Java", "Pandas"]
    print("Actual List: ",technology)
    
    # Consider list of indexes to remove
    indexes=[2,4]
    
    # Remove multiple elements from list
    for i in sorted(indexes, reverse=True):
        del technology[i]
    print("Final List: ",technology)
    
    # Output:
    # Actual List:  ['Hadoop', 'Spark', 'Python', 'Java', 'Pandas']
    # Final List:  ['Hadoop', 'Spark', 'Java']
    

    <a id="h-7-using-enumerate-loop"></a>

    7. Using enumerate() + Loop

    You can use enumerate() and a loop in Python to remove elements from a list based on a condition. For example, the loop uses enumerate() to iterate over the elements in the list numbers. For each iteration, the i variable holds the index of the current element and the num variable holds the value of the current element. In the end, the num list holds all the elements from numbers except the ones at the indices specified in indexes.

    
    # Using enumerate() + loop
    numbers = [2, 5, 8, 4, 1, 7, 3, 9]
    print("Actual List: ",numbers)
    indixes=[2,4,6]
    
    num = []
    for ind, ele in enumerate(numbers):
        # checking if element not present in index list
        if ind not in indixes:
            num.append(ele)
    print("Final List: ",num)
    
    # Output:
    # Actual List:  [2, 5, 8, 4, 1, 7, 3, 9]
    # Final List:  [2, 5, 4, 7, 9]
    

    <a id="h-8-using-enumerate-list-comprehension"></a>

    8. Using enumerate() + List Comprehension

    You can also remove elements from a list at a specific index using enumerate() and list comprehension. For example, the list comprehension uses enumerate() to loop through the elements in the numbers list and checks the index against the indixes list. If the index is not in the indexes list, the element is added to the num list. The result is a new list num containing the elements of numbers that were not removed.

    
    # Remove element by index 
    # Using enumerate() + List Comprehension
    numbers = [2, 5, 8, 4, 1, 7, 3, 9, 14]
    print("Actual List: ",numbers)
    indixes=[2, 4, 6, 8]
    # Using enumerate() + List Comprehension
    num = [ele for idx, ele in enumerate(numbers) if idx not in indixes]
    print("Final List: ",num)
    
    # Output
    # Actual List:  [2, 5, 8, 4, 1, 7, 3, 9, 14]
    # Final List:  [2, 5, 4, 7, 9]
    

    Conclusion

    In this Python article, I have explained how to remove an element/item from a list by index and remove multiple elements by using methods like list.remove(), del statement, pop(), List comprehension, and enumerate() functions with examples.

    Happy Learning !!

    Related Articles

    References