Reputation: 11
I know they virtually do the same thing, but is their time complexity the same, too? Or does the version with -1 go through the entire list first?
I couldn't find a conclusive answer anywhere.
Upvotes: 0
Views: 519
Reputation: 155418
There is no difference; the code the implements list.pop
actually treats a failure to provide an argument as if you passed -1
, so the code paths (aside from parsing the argument in the first place, which isn't needed when no argument is given) are identical.
You don't even need to rely on the source code to prove this; the mutable sequence documentation explicitly says in a note for pop
:
The optional argument
i
defaults to-1
, so that by default the last item is removed and returned.
It's not an implementation detail that -1
is used, it's actually defined that way in the spec, rather than having a special case for no argument (which makes sense; who wants to write a special case code path when you could just reuse the same code path for all calls?).
Upvotes: 2