-4

I have been trying to work on this certain pattern for quite some time but I am unable to come up with a solution. For example I have list like: n = ['M', 'M', 'B', 'B'].

Now, I know how to remove the elements from a list that have odd indexes or even indexes like del n[1::2] for odd indexes and del n[0::2] for even indexes.

However, I am looking to remove in a pattern like this: 0, [1], 2, [3], [5], [8]..... so on and so forth. The brackets are for the elements in those certain indexes to be removed.

The n list is just an example, I have much larger lists with more elements. So far, all the things I have tried haven't worked and I would appreciate any help on this. Thanks.

Mahir Islam
  • 1,941
  • 2
  • 12
  • 33
  • 1
    What's the pattern behind `0, 1, [1], 2, [3], [5], [8]`? Obviously you are talking about the fibonacci sequence, but what classifies an element as an "index to be removed"? – offeltoffel Jan 28 '20 at 13:53
  • 1
    Please specify what's the pattern you want to delete the elements. – Ch3steR Jan 28 '20 at 13:53
  • Does this answer your question? [Deleting multiple elements from a list](https://stackoverflow.com/questions/497426/deleting-multiple-elements-from-a-list) – Nathan Jan 28 '20 at 13:55
  • Presumably `0, 1, [1], 2 ...` is a typo and should read `0, [1], 2 ...` – jarmod Jan 28 '20 at 13:57
  • @jarmod thanks, I will be editing it – Mahir Islam Jan 28 '20 at 13:59
  • 1
    @jarmod: I doubt that. 0,1,1,2,3,5,8 is the fibonacci sequence. But the indices to remove seem to have no particular order. There is no algorithm that follows "whatever assumption", so we need to know this information ... Edit: question has been edited. Now the list makes even less sense. Please enlighten us, Mahir – offeltoffel Jan 28 '20 at 13:59

1 Answers1

1

You could use this:

orig_list = [1, 2, 3, 4, 5]
indices_to_retain = [0, 1, 4]

new_list = [content for entry, content in enumerate(orig_list) if entry in indices_to_retain]
# [1, 2, 5]
Carsten
  • 2,765
  • 1
  • 13
  • 28