+ 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

23rd Jan 2020, 7:22 PM
ren[paused]
ren[paused] - avatar
11 Antworten
+ 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.
23rd Jan 2020, 7:33 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
But this: print(a.append(a.pop())) Prints None because the append method is not supposed to return any value.
23rd Jan 2020, 7:50 PM
Seb TheS
Seb TheS - avatar
+ 3
ren that's weird, can you show us your code ? Write it in code playground, so we can test it there.
23rd Jan 2020, 7:36 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
ren no problem 😁👍👌
23rd Jan 2020, 7:45 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
What? For me this worked: lst = list(range(10)) lst.append(lst.pop()) #lst equals list(range(10)) like it's supposed to.
23rd Jan 2020, 7:31 PM
Seb TheS
Seb TheS - avatar
+ 2
I got it now I think :) thumbs up to all the helpers!!
23rd Jan 2020, 8:30 PM
ren[paused]
ren[paused] - avatar
+ 1
Yes I had either one of these two effects- either I'd get NONE or the list remains unchanged.
23rd Jan 2020, 7:35 PM
ren[paused]
ren[paused] - avatar
+ 1
Apologies I thought I inserted code
23rd Jan 2020, 7:37 PM
ren[paused]
ren[paused] - avatar
+ 1
It seems that I didnt fully understand what happens with pop without arguments
23rd Jan 2020, 7:42 PM
ren[paused]
ren[paused] - avatar
+ 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.
23rd Jan 2020, 7:45 PM
Seb TheS
Seb TheS - avatar
+ 1
The append method is of class Nonetype so it returns None
25th Jan 2020, 11:44 AM
John
John - avatar