Python – Print list after removing element at given index


Python Language is the most powerful programming language in dealing with the data. And list is a type of data structure among the different types available in Python. The list can hold elements of different data objects like integers, strings or even float types. The values which are assigned to the list once cannot be changed later. The elements in the list are identified using the index value and, in this article, we are going to deal with the removal of elements by specifying the index.

Print list after removing element at given index

The list is composed of elements and the index value starts from 0 to the number of elements mentioned. The removal of elements from the list can be explained with pictorial representation. There are different approaches performed for removing the elements. The list given below holds 7 elements and first row indicates the element’s index value and the second row contains the values of the elements.

Removing the element in index”3”

To remove the element, we need to locate the third index of the list array, which is β€œ56”. And after removing the mentioned element, the new list is given below,

Approach

  • Approach 1 βˆ’ Using the pop() function

  • Approach 2 βˆ’ Using the numpy module

  • Approach 3 βˆ’ Using the reduce method

Approach 1: Python program to print list after removing element at a given index using pop() function

The above method is simply done with the help of pop() function to remove the element from the list. The below code can be used to remove the element by replacing the pop() function with del() function. In case of remove() method, the elements are removed, by directly mentioning the element as a parameter to the function.

Algorithm

  • Step 1 βˆ’ The list is created to hold the integer, string and float value together and assigned to a variable named list1.

  • Step 2 βˆ’ The pop() function is used to remove the element at the particular index of 2 and 0

  • Step 3 βˆ’ Finally, the print function returns the new list.

Example

#initializing the list with set of elements
list1 = [10, 20, 30, 'Hello ', 67.9]
#The pop function will simply remove the element for the mentioned index value
#index value of 2 and 0 needs to be removed
list1.pop(2)
list1.pop(0)
#print function will return the list after removing the elements.
print(list1)

Output

[20, 'Hello ', 67.9]

Approach 2: Python Program to print list after removing element at given index using numpy module

The elements can be removed using the simple methods, when the dataset is larger, we can switch to the builtin-functions from the respective modules. In this case, numpy module is used.

Algorithm

  • Step 1 βˆ’ The required module for removing the element is numpy and declared here as β€œnp”.

  • Step 2 βˆ’ The list data structure with integers, strings and float numbers are created.

  • Step 3 βˆ’ To remove the element using the numpy module, the given list needs to be converted into numpy array.

  • Step 4 βˆ’ The converted numpy array is stored in a variable named β€œval”.

  • Step 5 βˆ’ The function np.delete() is used to remove the element from the numpy array when the index value is mentioned.

  • Step 6 βˆ’ Then the above function is declared to new variable and the new list is printed.

Example

#importing the module as np
import numpy as np
#Creating a list to hold values of different data types
list1 = [10, 20, 30, 'Hello ', 67.9]
#To convert the list data structure to numpy array and stored in val
val = np.array(list1)
#The given index value elements are removed.
#The delete function has two parameters 
newlist = np.delete(val, [0,2])
#Then finally returns the new list
print(newlist)

Output

['20' 'Hello ' '67.9']

Approach 3: Python Program to print list after removing element at given index using reduce method

The functools module is used to remove the element from the list using the reduce function from this module.

Algorithm

  • Step 1 βˆ’ From the functools module, the reduce method is imported

  • Step 2 βˆ’ The list is created with a list of elements.

  • Step 3 βˆ’ The lambda function is used, which is an anonymous function which needs not be defined

  • Step 4 βˆ’ The print statement returns the list after removing elements in 0 and 2.

Example

#importing the module
from functools import reduce
#Creating a list to hold values of different data types
list1 = [10, 20, 30, 'Hello ', 67.9]
#The elements removed using reduce method with key parameters.
new_list = reduce(lambda a,b: a+[b] if list1.index(b) not in [0,2] else a, list1, [])
#Then finally returns the new list
print(new_list)

Output

[20, 'Hello ', 67.9]

Conclusion

The organization must deal with large dataset, so removing the unwanted element makes the work efficient and easier. With the help of Python language, it is made possible; to remove the element from the list using del, remove, pop, numpy module and functools module.

Updated on: 04-Sep-2023

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements