Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 961
shelper Avatar asked Jan 04 '21 02:01

shelper


People also ask

What happens if you pop an empty list?

pop method removes the item at the given position in the list and returns it.

How do you reverse a list in pop Python?

One more way to reverse the list in python is by using two built-in functions append() and pop(). This works by poping the last element from the original list and appending it to the new list.

How do you reverse a list without reverse?

In order to reverse a list without using the built-in reverse() function, we use the Slicing Operator. The slicing operator is another method used for reversing the data elements.

How to create a reversed copy of an existing list?

If you want to create a reversed copy of an existing list in Python, then you can use reversed (). With a list as an argument, reversed () returns an iterator that yields items in reverse order: In this example, you call reversed () with digits as an argument. Then you store the resulting iterator in reversed_digits.

What is the difference between list() and reversed()?

As you already know, the call to list () consumes the iterator that results from calling reversed (). This way, you create a new list as a reversed copy of the original one. Python 2.4 added reversed (), a universal tool to facilitate reverse iteration over sequences, as stated in PEP 322.

How to reverse the contents of a list in Python?

Method 2: Using the reverse () built-in function. Using the reverse () method we can reverse the contents of the list object in-place i.e., we don’t need to create a new list instead we just copy the existing elements to the original list in reverse order. This method directly modifies the original list.

Does reverse() return a new list?

Note that .reverse () doesn’t return a new list but None: Trying to assign the return value of .reverse () to a variable is a common mistake related to using this method. The intent of returning None is to remind its users that .reverse () operates by side effect, changing the underlying list.


2 Answers

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.

like image 161
M Z Avatar answered Sep 20 '22 04:09

M Z


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) [] >>>  
like image 27
Copperfield Avatar answered Sep 19 '22 04:09

Copperfield