Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

394
Views
Impact of removing a list item on reversed() in python

As far as I know, reversed() function gives an iterator and works just like iter() but will give the items in reverse order. However I faced a strange behavior from the object that gets back from reversed() function.

By looking at:

lst = ['a', 'b', 'c', 'd']
iter_lst = iter(lst)
lst.remove('c')
print(list(iter_lst))

output : ['a', 'b', 'd']

It's just as expected. but:

lst = ['a', 'b', 'c', 'd']
rev_iter_lst = reversed(lst)
lst.remove('c')
print(list(rev_iter_lst))

output : []

Shouldn't it be : ['d', 'b', 'a'] ?

Is it something in implementation of __reversed__() method in list object or __next__() method in the iterator object that prevents this ? I mean if something changes in original list it won't produce reverse sequence maybe...

Update: I've posted an answer which is a possible fix to it here, I've tested that but I'm unaware if there is a situation that this implementation would give unexcepted result.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

according to list.__reversed__ source code, the iterator remembers the last index address and returns the iterator which remembers the last index address. now when you remove an item it will shift all the indexes and makes the last address point to nowhere and it will return an empty list because there is nothing to iterate over. let me describe more: consider following list: lst = ['a','b','c'] also let's assume lst[0] is at the 100 and each character is one byte so, the character 'c' is in the 102. when you create a revered iterator it will remember 102 as the start point. in the next step, we omit 'b' now the character 'c' is in address 101. and when you ask the iterator to iterate, it will start to look at position 102. what it will found there? literally nothing and obviously it will return an empty list.

I hope this can be helpful :)

EDIT: the word address is not correct. I must use index instead...

over 4 years ago · Santiago Trujillo Report

0

The list call will iterate the reverse iterator, whose index < PyList_GET_SIZE(seq) check here will fail because you shrunk seq in the meantime, and thus won't yield a value but stop:

listreviter_next(listreviterobject *it)
{
    (some checks)
    index = it->it_index;
    if (index>=0 && index < PyList_GET_SIZE(seq)) {
        (decrease the index and return the element)
    }
    (stop the iteration)
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!