-2

I have a list like this lst = [1,3,5] and a major list like lost =['the' , 'cat' , 'thinks' , 'you' , 'are' , 'crazy' ]

I want to delete elements in second list based on indexes in the first one . It means i have to remove 'cat' , 'you' and 'crazy' .

The problem is if i use :

lost.remove(lost[1])
lost.remove(lost[3])
lost.remove(lost[5])

First problem is its not gonna work out! Because as we remove the first element the length of the list(named lost) decreases and in that way we will remove wrong elements.

Second problem is the list named(lst) will not always be [1,3,5] . Its gonna change in length and in elements .

How can i fix the problem?

7 Answers7

1

Your're not creating a list with {}, but a set. If you want to create a list you need to use the [] characters. After that you can remove the elements from the list like so:

indexes = {1,3,5}
lst = ['the' , 'cat' , 'thinks' , 'you' , 'are' , 'crazy']

lst_dct = dict(enumerate(lst))
for index in indexes:
    lst_dct.pop(index)

new_lst = list(lst_dct.values())

The new_lst will now contain the remaining elements.

Also instead of the remove function, which requires an element, you need to use pop to remove and element from a list based on the index.

Martijn Bots
  • 366
  • 1
  • 5
1

As @np8 commented, you can remove the elements in descending index order like the following:

lst = [1, 3, 5]
lost = ['the', 'cat', 'thinks', 'you', 'are', 'crazy']

for index in reversed(lst):  # descending index order
    del lost[index]

print(lost)

which prints

['the', 'thinks', 'are']

UPDATE (Thanks to @wwii for the comment)

If the given lst is not sorted in ascending order, you can do like the following instead:

lst = [3, 1, 5]
lost = ['the', 'cat', 'thinks', 'you', 'are', 'crazy']

for index in sorted(lst, reverse=True):  # descending index order
    del lost[index]
Gorisanson
  • 2,202
  • 1
  • 9
  • 25
0

Every time you delete an element from your list, say according to lost[i], traverse through lost from i to the end, and decrement all the values by 1.

0

You can solve it using list comprehension like below:

lst = [1,3,5]
lost =['the' , 'cat' , 'thinks' , 'you' , 'are' , 'crazy' ]

print([ val for idx, val in enumerate(lost) if idx not in lst])

It would be : ['the', 'thinks', 'are'] Hope helps you

Brad Figueroa
  • 869
  • 6
  • 15
  • Does your solution work specifically because it is a list comprehension or can a for loop work? Does it work because it creates a new list instead of removing items from the original? – wwii Jul 24 '20 at 13:59
  • It'll work with a `for loop` as well, but you can solve it in a one-liner code. The main reason is that when you wanna delete items by their index you're gonna get in trouble because the length list changes every time – Brad Figueroa Jul 24 '20 at 14:07
0

You can use list comprehension with condition too .

lst = [1,3,5]
lost =['the' , 'cat' , 'thinks' , 'you' , 'are' , 'crazy' ]
copy_list=[lost[i] for i in range(len(lost)) if not i in lst]
  • Can you do it with a for loop or does it work specifically because it is a list comprehension? – wwii Jul 24 '20 at 14:00
  • @wwii It could work for a loop too, just would be longer to write! List comprehension in the underlying layer implements the loop and makes the code readable. – Gaurav Singh Jul 25 '20 at 16:00
0

If space is not an issue, you can use this:

import numpy as np

lst = [1, 3, 5]
lost =['the' , 'cat' , 'thinks' , 'you' , 'are' , 'crazy' ]
output = np.delete(lost, lst).tolist()
print(output)

output:

['the', 'thinks', 'are']
Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16
0

Loop over indexes Append the ones that are not required


new = [lost[i] for i in range(len(lost)) if i not in lst]
   



InfoLearner
  • 14,952
  • 20
  • 76
  • 124
  • Better: Use `enumerate` like this: `new = [word for index, word in enumerate(lost) if index not in lst]`. If `lst` is big you might want to make a `set` from it first to speed things up. – Matthias Jul 24 '20 at 15:35