KJAMIX
KJAMIX

Reputation: 1

Why is the pop function not popping the correct index, after the append function?

So I was working on this exercise: In a given sequence the first element should become the last one. An empty sequence or with only one element should stay the same. So I quickly wrote a function which used append then pop functions to add the first element to the end. Then pop the first element.

from collections.abc import Iterable

def replace_first(x: list) -> Iterable:
    x.append(x[0])
    x.pop(x[0])
    return x

print("Example:")
print(list(replace_first([1, 2, 3, 4])))

So when I ran the code it returned this:

Example:
[1, 3, 4, 1]

What I was expecting:

Example:
[2, 3, 4, 1]

I thought that was strange because my assumption is that 2 would still have the index of [1] after append. So I tested this by printing the all elements of the new list by the index after append.

from collections.abc import Iterable

def replace_first(x: list) -> Iterable:
    x.append(x[0])
    print(x[0])
    print(x[1])
    print(x[2])
    print(x[3])
    print(x[4])
    x.pop(x[0])
    return x

print("Example:")
print(list(replace_first([1, 2, 3, 4])))  

Which returned

Example:
1
2
3
4
1
[1, 3, 4, 1]

So I'm a little confused on what exactly is happening. What I had printed show the new index is what I was expecting. That 2 has the index [1], but when using the pop function on index [0] it removes [1].

I know that the exercise can be completed this way,

from collections.abc import Iterable

            
def replace_first(x: list) -> Iterable:

    if x:
        x.append(x.pop(0))  
    return x

But I'm trying to understand what's going on with the pop function in the instance above. Any explanation or recommended reading will be appreciated. Thanks in advance.

Upvotes: -1

Views: 34

Answers (1)

Barmar
Barmar

Reputation: 782166

When you have

x = [1, 3, 4, 1]

the value of x[0] is 1. So

x.pop(x[0])

is equivalent to

x.pop(1)

Since the argument list.pop() is the index of the element to remove, not the value, this removes the second element of the list. So it removes 3, not 1.

You must use x.pop(0) instead of x.pop(x[0]), just like you do in the shorter version at the end of the question.

A method that removes by value is list.remove(), so

x.remove(x[0])

would do what you want, but it's a little less efficient because it has to search for the value (although the search is guaranteed to be short since it's the first element).

Upvotes: 3

Related Questions