how to modify a list while iterating (intermediate) anthony explains #402

how to modify a list while iterating (intermediate) anthony explains #402

anthonywritescode

2 года назад

9,689 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Meshi
Meshi - 04.09.2023 16:14

Using remove() would only work with unique values right? In those cases does enumerating and pop(index) work just as well?

Ответить
AA
AA - 26.08.2023 19:16

I was Hoping you would have a trick that doesn’t have space overhead for dict😂

Ответить
bart tea
bart tea - 16.02.2023 12:43

this is a cool solution but I think for lists that aren't sorted and have duplicate values it will still have unexpected behavior because lst.remove will remove first matching value left to right so it could be better to use lst.pop(index) like so:
def delete_zeros(lst):
i = -1
for num in reversed(lst):
if num == 0:
lst.pop(i)
i += 1
i -= 1
return lst

delete_zeros([1, 0, 0, 1, 0, 2, 0, 1, 3])

Ответить
The Foliot
The Foliot - 04.10.2022 02:49

you just solved my problem and in a palatable way. Subscribed. Thanks.

Ответить
mark lagana
mark lagana - 01.07.2022 09:31

what about using a pointer to iterate over the list, and if you remove an item, decrement pointer, else increment the pointer. Like so:

it = 0
end = len(list)

while(it < end):
if (condition_is_met):
del list[it]
end -= 1
it -= 1
else:
it += 1

Ответить
Blumer
Blumer - 03.05.2022 21:13

Few years ago I had a loop that iterated through a set while deleting some elements and it has really chaotic behavior, not even predictable like the list iterator skipping one element

It was hell to debug because it worked normally most of the time, and when I found it I didn't know this was something one shouldn't do so I thought it was some kind of interpreter bug, ended up in the set iterator implementation cpython code and found a nice comment explaining that it would be a really bad idea to modify the set while using the iterator 😅

Ответить
Fabiolean
Fabiolean - 25.04.2022 01:21

I've started using

for item in list(my_list):
# modify list here

since list() returns a copy of the list.

Ответить
Ludo
Ludo - 16.04.2022 03:57

I did this once and i only realized later i wasn't iterating over a copy of the list. I was appending new elements depending on the elements already in the list. That would have to happen to the new elements too until no new elements are appended. In the end this behavior was actually beneficial somehow lol. I can't vouch for the correctness of that alg though. But iirc i managed to pass all the tests that alg had to pass (in a academic exam, let's say).

I guess my problem was different though, 'cuz i think i didn't do any removing.

Ответить
_scourvinate
_scourvinate - 03.03.2022 11:44

Personally my first idea was to use a while loop and use an if statement to either remove or indent the index. Iterating backwards is a really interesting solution, I like it.

Ответить
Muhammad Sarim Mehdi
Muhammad Sarim Mehdi - 03.03.2022 03:43

is there any situation where iterating over a list and removing elements from it is a good idea? I was always under the impression that you should never do that

Ответить
Someone Anonymous
Someone Anonymous - 03.03.2022 01:53

Would add to the list while iterating also show some kind of error?

Ответить
Adrian González
Adrian González - 02.03.2022 23:26

Do you have a video about closure?

Ответить
Tushar Sadhwani
Tushar Sadhwani - 02.03.2022 21:50

I learned this trick first from CodingTrain 6 years ago! Still amazes me that this isn't common programming knowledge.

Ответить
Luka Jeličić Lux
Luka Jeličić Lux - 02.03.2022 21:17

Weird stuff, I must admit it's even weirder than print( condition and true value or false value ) code.

Ответить
tripleX redemption
tripleX redemption - 02.03.2022 20:18

thanks!!

Ответить
Lonterel
Lonterel - 02.03.2022 19:34

But reversed also returns a new list, so using list() or reversed() does not matter

Ответить
suayip uzulmez
suayip uzulmez - 02.03.2022 19:34

I presume reversing a list also has some overhead, but not much as copying it?

Ответить
Ian Chui
Ian Chui - 02.03.2022 19:22

This is a huge throwback, I remember you telling me to iterate backwards back in 2018 when I was running into this exact issue, have never forgotten this strat

I had an interview a couple months back where I told the dude that attempting to remove while iterating could become messy and had to explain why cause he had no idea :p

i love i have these random moments perma locked in memory lmaooo

Ответить