How to Remove an Element from a List by Index

How to remove an element from a list by index

Use del and specify the index of the element you want to delete:

>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del a[-1]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8]

Also supports slices:

>>> del a[2:4]
>>> a
[0, 1, 4, 5, 6, 7, 8, 9]

Here is the section from the tutorial.

Removing elements from a list by index from another list of integers

This should do what you want (delete from details every element indexed by the values in num):

for i in reversed(num):
del details[i]

It iterates over the list backwards so that the indexing of future things to delete doesn't change (otherwise you'd delete 3 and then the element formerly indexed as 22 would be 21)--this is probably the source of your IndexError.

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, 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)

How can I remove a specific item from an array?

Find the index of the array element you want to remove using indexOf, and then remove that index with splice.

The splice() method changes the contents of an array by removing
existing elements and/or adding new elements.

const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
array.splice(index, 1); // 2nd parameter means remove one item only
}

// array = [2, 9]
console.log(array);

How can I remove specific index from list in python

Please review your question again. Please correct your question because it is unclear what you are trying to achieve. These are the two conditions that i think you wish to achieve.

  1. if list[0] == 1 then remove that element.
  2. if list[i+1]-1 == list[i] then remove list[i+1]

First of all in line 6 of your code there is an error, it should be if == and not if =.The following code will achieve the above conditions.

numbers = int(input("Enter the limit for the list : "))

list_num = []

for i in range(0,numbers):
list_num.append(int(input("list["+str(i)+"]: ")))

if list_num[0] == 1:
list_num.remove(list_num[0])
try:
for i in range(0,len(list_num)):
if list_num[i] == list_num[i+1]-1:
list_num.remove(list_num[i+1])
except:
print list_num

Input: [1,2,7,8,13,20,21]

Output: [2,7,13,20]

how to remove the element in list without knowing the index using python

you can search the list for the index of b and then use that as the start point for searching for a's index

l=["a","b","a","a","a"]

b_index = l.index("b")
print("b index is", b_index)

first_a_after_b = l.index("a", b_index)
print("first a after b is at index", first_a_after_b)

del l[first_a_after_b]
print(l)

OUTPUT

b index is 1
first a after b is at index 2
['a', 'b', 'a', 'a']

delete list element by index while iterating using python

Build a new list with a comprehension:

x = [element for (i,element) in enumerate(x) if i not in temp]

If you want to remove only duplicates, i.e. leaving one copy of the original, there is a better way to do that:

from collections import OrderedDict
x = list(OrderedDict.fromkeys(x))

Remove items from list according to a list of indexes

Probably the most functional (and Pythonic) way would be to create a new list, omitting the elements at those indices in the original list:

def remove_by_indices(iter, idxs):
return [e for i, e in enumerate(iter) if i not in idxs]

See it in action here.

How remove elements in array with out messing with the counter or list length

Maybe you could try this snippet to see that helps?

Have not done too many edge cases - so please raise questions, if run into some edges.

def delete_nth(lst, N):

seen = {}
res = []

for x in lst:
if x not in seen :
seen[x] = 0
else:
seen[x] += 1

if seen[x] <N:
res.append(x)
return res

print(delete_nth([1, 1, 1, 1], 2)) # [1, 1]
print(delete_nth([20, 37, 20, 22], 1)) # [20, 37, 22]


Related Topics



Leave a reply



Submit