0
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']

i want to remove color [0,4,5] so the output will be :

color ['green', 'White', 'Black']

what should I write?

azro
  • 53,056
  • 7
  • 34
  • 70
Avi Deri
  • 11
  • 3
  • For reference: https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists – Ukiyo Nov 21 '20 at 13:46
  • Does this answer your question? [How to remove an element from a list by index](https://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index) – Red Nov 21 '20 at 15:21

6 Answers6

2

You can use a list comprehension along with enumerate:

colors = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
indices = [0, 4, 5]
indices_set = set(indices)
filtered = [color for i, color in enumerate(colors) if i not in indices_set]
print(filtered) # ['Green', 'White', 'Black']
Aplet123
  • 33,825
  • 1
  • 29
  • 55
0

You'll have to subtract the position of the element you want to remove since every time you remove an element the length of the list is reduced giving other elements to the given positions,

color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
color.pop(0)
color.pop(4-1)
color.pop(5-2)
print(color)
Tharu
  • 188
  • 2
  • 14
0

Shortest code for your goal. The print statements are to show it works.

color  = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
remove = [0,4,5]

remove.reverse()

for x in remove:
                print(x)
                color.pop(x)
print(color)

If the numbers in the removal list is changed (e.g. [0,5,4]) you can sort first:

remove.sort(reverse=True)
[color.pop(x) for x in remove]
ZF007
  • 3,708
  • 8
  • 29
  • 48
0
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']

list(map(color.pop, reversed([0, 4, 5])))

print(color)
['Green', 'White', 'Black']
Henry Tjhia
  • 742
  • 1
  • 5
  • 11
  • This is wrong and slow. It works in this specific example because the indices `[0, 4, 5]` happen to be ordered and unique. But what if `to_drop = [4, 0, 5, 0]`? Further, this kind of in-place modification is discouraged (can have nasty side-effect if used on the argument of a function, without `.copy()` first). As for speed, if the list has 1M elements and `to_drop` has 10K, for example, this takes 20x more time than @Aplet123 correct answer. – Pierre D Nov 21 '20 at 16:46
  • @PierreD 1) This just an alternative. 2) The solution still provides a correct answer based on the original question. 3) The problem is oversimplified. 4) I by no mean state this better than others. 5) While I'm fully understand the inherented problem of this approach, I would like to thank you for explaining it. – Henry Tjhia Nov 21 '20 at 17:16
0

Note: This is essentially the same answer as @Aplet123, but with a few additional details.

  1. Concise (for your specific case):
[v for i, v in enumerate(colors) if i not in {0, 4, 5}]
  1. More general:
def drop(lst, to_drop):
    drop_idx = frozenset(to_drop)
    return [v for i, v in enumerate(lst) if i not in drop_idx]
  1. Speed test:
colors = list(np.random.uniform(size=1_000_000))
to_drop = list(np.random.choice(np.arange(len(colors)), replace=False, size=10_000))

%timeit drop(colors, to_drop)
# 68.4 ms ± 233 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Pierre D
  • 24,012
  • 7
  • 60
  • 96
-1
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
inc =[0,4,5]
fil=list(set(color)-set([color[x] for x in inc ]))
print(fil)
  • i dont want to name the color. the purpose is that every color in this location [0,4,5] will be always removed from the list without a name it . – Avi Deri Nov 21 '20 at 14:05
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Nov 21 '20 at 18:07
  • i make list from indicies of from color list ``` [color[x] for x in inc ]``` cast them to set both then subtract the two sets and re cast them to list you have the answer okay this is much easier ```list(set(color)-set([color[x] for x in inc ]))``` sorry I don't have much experience on stackoverflow – ubaidh khatlani Nov 21 '20 at 19:31