+ 1
Why does directly calling an index with pop work but with no arguments return none?
I am rotating a simple list with lst.append(lst.pop(0)) Lst is just a populated list with [0,1,2,3. . .9] Why does lst.append(lst.pop()) not work? https://code.sololearn.com/clfFBUGFk691/?ref=app
11 Réponses
+ 6
ren if you mean why the list doesn't change, it's because pop() with no arguments returns the last value.
So what you're doing is:
1. Removing the last value of the list, which is 9
2. Adding the last value to the end of lst, which is also 9
So the result, nothing wil change.
+ 4
But this:
print(a.append(a.pop()))
Prints None because the append method is not supposed to return any value.
+ 3
ren that's weird, can you show us your code ? Write it in code playground, so we can test it there.
+ 3
ren no problem 😁👍👌
+ 2
What? For me this worked:
lst = list(range(10))
lst.append(lst.pop())
#lst equals list(range(10)) like it's supposed to.
+ 2
I got it now I think :) thumbs up to all the helpers!!
+ 1
Yes I had either one of these two effects- either I'd get NONE or the list remains unchanged.
+ 1
Apologies I thought I inserted code
+ 1
It seems that I didnt fully understand what happens with pop without arguments
+ 1
ren If you call pop method without arguments it will use default value of -1 (index of last item).
You can find this information using help function:
help(list.pop), you'll get:
pop(self, index=-1, /)
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
where -1 refers to the default value of index.
+ 1
The append method is of class Nonetype so it returns None