Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
Why does popping from the original list make reversed(original_list) empty?

I have the following code:

s = [1,2,3]
t = reversed(s)

for i in t:
    print(i)
# output: 3,2,1

If I pop one element from s (original), then the t (reversed) is emptied:

s = [1,2,3]
t = reversed(s)
s.pop()

for i in t:
    print(i)
# expected output: 2, 1
# actual output (nothing): 

Why does this happen?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

calling reversed return a iterator over that list, which a special object that allow you iterate in reverse order over the original list, is not a new list and is a one time use only

>>> s= [1,2,3]
>>> t = reversed(s)
>>> t
<list_reverseiterator object at 0x00000261BE8F0C40>
>>> list(t)
[3, 2, 1]
>>> list(t)
[]
>>> 

and because this iterator reference the original list, any change on it is reflected when you iterate over the iterator later.

Update

In particular and as MZ explain, if that change is such that the state of the list is different from when the iterator was created you get nothing if the size decreases or an incomplete version of the list if increased

>>> s= [1,2,3]
>>> t = reversed(s)
>>> s.insert(0,23)
>>> s
[23, 1, 2, 3]
>>> list(t)
[2, 1, 23]
>>> t = reversed(s)
>>> s.append(32)
>>> list(t)
[3, 2, 1, 23]
>>> s
[23, 1, 2, 3, 32]
>>> t = reversed(s)
>>> s.pop()
32
>>> list(t)
[]
>>> 
over 4 years ago · Santiago Trujillo Relatório

0

Taking a look at the cpython code on GitHub, we can get some intuition as to why it no longer works.

The iterator that is returned essentially requires knowing the position of the last index and the length of the array. If the size of the array is changed, the iterator will no longer work.

Test 1: Increasing the array length

This will not produce the correct results either, but the iterator does run:

s = [1,2,3]
t = reversed(s)
s.append(4)

for i in t:
    print(i)
# output: [3, 2, 1]

Test 2: Decreasing, then increasing the length

s = [1,2,3]
t = reversed(s)
s.pop()
s.append(4)

for i in t:
    print(i)
# output: [4, 2, 1]

It still works!

So there's an internal check to see whether or not the last index is still valid, and if it is, it's a simple for loop down to index 0.

If it doesn't work, the iterator returns empty.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda