0

Although this should be rather easily done with a for loop, I'm wondering wether there is a concise way to delete a set of positions from a Python list. E.g.:

l = ["A","B","C","D","E","F","G"]
pos = [4,6]
# Is there something close to
l.remove(pos)

Best

dputhier
  • 734
  • 1
  • 7
  • 23

1 Answers1

1

del is what you are looking for if you have consecutive indices:

From the documentation:

There is a way to remove an item from a list given its index instead of its value: the del statement

Otherwise, you could generate a new list, by filtering out indices, which you aren't interested in

result = [x for i, x in enumerate(l) if i not in pos]
Thomas Junk
  • 5,588
  • 2
  • 30
  • 43