Open In App

Python | Print list after removing element at given index

Last Updated : 15 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how we can remove elements at a given index and print the list afterward.

Example

Input1: list = [10, 20, 30, 40, 50] 
             index = 2
Output1: [10, 20, 40, 50] 
Input2: list = [10, 20, 40, 50] 
             index = 0 
Output2: [20, 40, 50] 

Print list after removing element at given index

Below are the methods by which we can remove element at a given index in a list in Python

  • Remove and Display List
  • Using pop()
  • del function
  • Using reduce()
  • Using NumPy module

Remove and Display List Element at Position

Using traversal in the list, we will append all the index values except the given index to a new list and then print the new list. For this we will require a new list where we can append all the values except the given index value.

Python3




def remove(list1, pos):
    newlist = []
 
    # traverse in the list
    for x in range(len(list1)):
         
        # if index not equal to pos
        if x != pos:
            newlist.append(list1[x])
    print(*newlist) 
 
# driver code
list1 = [10, 20, 30, 40, 50]
pos = 2
remove(list1, pos)


Output

10 20 40 50


Removing Element at Given Index in Python List using pop()

pop() function helps us to pop the value at any desired position that is passed in the parameter, if nothing is passed in the parameter, then it removes the last index value. In this example, we are using pop().

Python3




def remove(list1, pos):
    # pop the element at index = pos
    list1.pop(pos)
    print(*list1)
     
# driver code
list1 = [10, 20, 30, 40, 50]
pos = 2
remove(list1, pos)


Output

10 20 40 50


Python Remove Elements from a List Using del Keyword

del Keyword can be used to remove any element at any given position. If -1 or -2 is given in the [] brackets, then it deletes the last and second last element respectively. In this example, we are using del keyword.

Python3




def remove(list1, pos):
 
    # delete the element at index = pos
    del list1[pos]
    print(*list1)
      
# driver code
list1 = [10, 20, 30, 40, 50]
pos = 2
remove(list1, pos)


Output

10 20 40 50


Remove Elements in a List Using reduce()

In this example, we are using reduce() to remove an element at a particular index.

Python3




from functools import reduce
 
# list from which we have to remove the element
list1 = [20, 35, 'GFG ', 99, 33, 45, 23]
# implementing reduce() function
new = reduce(lambda a, b: a+[b] if list1.index(b)
             not in [0, 2] else a, list1, [])
print(new)


Output

[35, 99, 33, 45, 23]

Print List After Removing Element Using NumPy Module

In this example, we are using numpy.delete() function to remove an element at a given index in the Python List.

Python3




import numpy as np
 
def remove(list1, pos):
    arr = np.array(list1)
    # Deleting element at given position using numpy.delete() function
    new_arr = np.delete(arr, pos)
    # Converting NumPy array back to list
    new_list = new_arr.tolist()
    # Printing updated list
    print(*new_list)
 
# driver code
list1 = [10, 20, 30, 40, 50]
pos = 2
remove(list1, pos)


Output

10 20 40 50



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads