How to delete elements in a list using delete order?


Ding medicine

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

I want to remove elements in the second list based on the index in the first list. This 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])

The first problem is that it won't work! Because when we remove the first element, the length of the list ( named lost ) decreases, so that we will remove the wrong element.

The second problem is that the list named ( lst ) is not always [1,3,5]. It changes length and elements.

How can I fix this problem?

gorisanson

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

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 lstis not sorted in ascending order, you can do:

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

for index in sorted(lst, reverse=True):  # descending index order
    del lost[index]

Related


How to delete elements of a list in R?

Paio I have an igraph object which I created with the igraph library. This object is a list. Some components of this list have length 2. I want to remove all these components. IGRAPH clustering walktrap, groups: 114, mod: 0.79 + groups: $`1` [1] "OTU004

How to delete elements of a list in R?

Paio I have an igraph object which I created with the igraph library. This object is a list. Some components of this list have length 2. I want to remove all these components. IGRAPH clustering walktrap, groups: 114, mod: 0.79 + groups: $`1` [1] "OTU004

Rotate and delete elements in a list

Graziko I want to iterate through list A and remove from that list any element with the same key as the key pair located in list B, then create a new list C from it. I get that it only works for the head, but I don't know how to loop through the tail of list B

Rotate and delete elements in a list

Graziko I want to iterate through list A and remove from that list any element with the same key as the key pair located in list B, then create a new list C from it. I get that it only works for the head, but I don't know how to loop through the tail of list B

How to delete JSONArray elements using Java

yaryan997 : my JsonArray is [{ "Id": null, "Name": "One New task", "StartDate": "2010-02-03T05:30:00", "EndDate": "2010-02-04T05:30:00", "Duration": 1, "DurationUnit": "d", "PercentDone": 0, "ManuallyScheduled": false, "Priority": 1, "parentId": 8, "index": 0,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in csproj using PowerShell

Zahar I try to remove the element <ItemGroup>from the .csprojfile but my script does nothing. What am I doing wrong? script: [xml]$csproj = Get-Content -Path ".\PackageTest.csproj" $refs = $csproj.Project.ItemGroup | Where-Object {$_.Content.Include -like "Pk

How to delete JSONArray elements using Java

yaryan997 : my JsonArray is [{ "Id": null, "Name": "One New task", "StartDate": "2010-02-03T05:30:00", "EndDate": "2010-02-04T05:30:00", "Duration": 1, "DurationUnit": "d", "PercentDone": 0, "ManuallyScheduled": false, "Priority": 1, "parentId": 8, "index": 0,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to delete elements in an array using RethinkDB

Dazik I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "

How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

How to delete JSONArray elements using Java

yaryan997 : my JsonArray is [{ "Id": null, "Name": "One New task", "StartDate": "2010-02-03T05:30:00", "EndDate": "2010-02-04T05:30:00", "Duration": 1, "DurationUnit": "d", "PercentDone": 0, "ManuallyScheduled": false, "Priority": 1, "parentId": 8, "index": 0,

How to delete elements in an array using RethinkDB

Datsik: I have a table that Lobbysactually contains party rooms, it has an array of Members and an array of Messages, here is an example: { "id": "a77be9ff-e10f-41c1-8a4c-66b5a55d823c" , "members": [ "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" , "Gacnt" ,

How to remove attached elements using delete

username I have a simple code with 2 grids. A div contains a button called "Add". When clicked, additional buttons will be added in the second div. When you click any button in the second div it may delete itself. As you can see in the code below, I am using a

How to delete a list in a list?

Magruiz 1 I am trying to delete a list within a list, but my code fails to delete even after deleting the list. y = [45] x = [1,2,3,5,y] del y print(x) print out x: [1, 2, 3, 5, [45]] My output reads [1, 2, 3, 5, [45]], meaning list ystill exi

How to delete a list in a list?

National imprisonment I have a list like this[1, 2, 3, [4, 5, 6]] How can I remove the characters in the [list in order to get [1, 2, 3, 4, 5, 6]? Here is my code so far: a = [1, 2, 3, [4, 5, 6]] new_a = [] for item in a: if len(item) > 1: for sub_

How to delete a list in a list?

Magruiz 1 I am trying to delete a list within a list, but my code fails to delete even after deleting the list. y = [45] x = [1,2,3,5,y] del y print(x) print out x: [1, 2, 3, 5, [45]] My output reads [1, 2, 3, 5, [45]], meaning list ystill exi

How to delete a list in a list?

Magruiz 1 I am trying to delete a list within a list, but my code fails to delete even after deleting the list. y = [45] x = [1,2,3,5,y] del y print(x) print out x: [1, 2, 3, 5, [45]] My output reads [1, 2, 3, 5, [45]], meaning list ystill exi

How to delete an item in a list using WriteBatch?

vesii: I am using Firebase Cloud database. I queried a collection and got a list of documents. Each document contains a field containing a carsstring . If the array contains at least three strings, I want to remove that string carPathfrom the array (not the wh