This question already has answers here :
This question already has an answer here. How to delete elements from a list using a list of indexes?.
BTW this will do for you
x = [12, 45, 55, 6, 34, 37, 656, 78, 8, 99, 9, 4]
index_list = [0, 4, 11]
value_list = []
for element in index_list:
value_list.append(x[element])
for element in value_list:
x.remove(element)
print(x)
Delete elements from python array with given index of elements as list, This question already has an answer here. How to delete elements from a list using a list of indexes?. BTW this will do for you x = [12, 45, 55, 6, Given an index, remove the element at that index from the list and print the new list. Examples: Using traversal in the list, 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.
You could also use enumerate:
x = [12, 45, 55, 6, 34, 37, 656, 78, 8, 99, 9, 4]
index_list = [0, 4, 11]
new_x = []
for index, element in enumerate(x):
if index not in index_list:
new_x.append(element)
print(new_x)
Remove Elements From Lists | Python List remove() Method, How do you remove an element from a specific index in Python? numpy.append() : How to append elements at the end of a Numpy Array in Python; Find the index of value in Numpy Array using numpy.where() How to Reverse a 1D & 2D numpy array using np.flip() and [] operator in Python; Python: Check if all values are same in a Numpy Array (both 1D and 2D) Python Numpy : Select an element or sub array by index
You can sort the del_list
list in descending order and then use the list.pop()
method to delete specified indexes:
for i in sorted(del_list, reverse=True):
x.pop(i)
so that x
would become:
[45, 55, 6, 37, 656, 78, 8, 99, 9]
Python : How to remove multiple elements from list ? – thispointer.com, How do I remove multiple elements from a list in Python? Sort the array in a given index range; Delete an element from array (Using two traversals and one traversal) Queries for bitwise OR in the index range [L, R] of the given array; Queries for bitwise AND in the index range [L, R] of the given array; Find if a crest is present in the index range [L, R] of the given array
Difference between del, remove and pop on lists, How do you delete an element from an array in Python? Delete list element using various Python functions with examples given. You can also remove all the elements from the Python list. There is various function available in Python to delete or remove list elements in Python. These functions are remove (), del (), pop () and clear ().
Python Nested List, by value. The latter requires searching the list, and raises ValueError if no such value occurs in the list. Python list are quite popular and no matter what type of field one is coding, one has to deal with lists and its various applications. In this particular article, we discuss ways to separate odd and even indexed elements and its reconstruction join. Let’s discuss ways to achieve this. Using Naive method, this task can be performed by using
Remove multiple elements from a list in Python, How do I remove an element from a nested list in Python? Delete elements, rows or columns from a Numpy Array by index positions using numpy.delete() in Python; Delete elements from a Numpy Array by value or conditions in Python; Python: Find duplicates in a list with frequency count & index positions; Python : How to remove files by matching pattern | wildcards | certain extensions only ?
Comments it is because after deleting 0th and 4th index element total list size is not > 11, that's why it gives index out of range error
. Use x = [e for i, e in enumerate(x) if i not in index_list]
, well known pattern If the question already has an answer, then flag as a duplicate, do not answer.