Why do python lists have pop() but not push()

Python

Python Problem Overview


Does anyone know why Python's list.append function is not called list.push given that there's already a list.pop that removes and returns the last element (that indexed at -1) and list.append semantic is consistent with that use?

Python Solutions


Solution 1 - Python

Because "append" existed long before "pop" was thought of. Python 0.9.1 supported list.append in early 1991. By comparison, here's part of a discussion on comp.lang.python about adding pop in 1997. Guido wrote:

> To implement a stack, one would need > to add a list.pop() primitive (and > no, I'm not against this particular > one on the basis of any principle). > list.push() could be added for > symmetry with list.pop() but I'm not > a big fan of multiple names for the > same operation -- sooner or later > you're going to read code that uses > the other one, so you need to learn > both, which is more cognitive load.

You can also see he discusses the idea of if push/pop/put/pull should be at element [0] or after element [-1] where he posts a reference to Icon's list:

> I stil think that all this is best > left out of the list object > implementation -- if you need a stack, > or a queue, with particular > semantics, write a little class that > uses a lists

In other words, for stacks implemented directly as Python lists, which already supports fast append(), and del list[-1], it makes sense that list.pop() work by default on the last element. Even if other languages do it differently.

Implicit here is that most people need to append to a list, but many fewer have occasion to treat lists as stacks, which is why list.append came in so much earlier.

Solution 2 - Python

Because it appends; it doesn't push. "Appending" adds to the end of a list, "pushing" adds to the front.

Think of a queue vs. a stack.

http://docs.python.org/tutorial/datastructures.html

Edit: To reword my second sentence more exactly, "Appending" very clearly implies adding something to the end of a list, regardless of the underlying implementation. Where a new element gets added when it's "pushed" is less clear. Pushing onto a stack is putting something on "top," but where it actually goes in the underlying data structure completely depends on implementation. On the other hand, pushing onto a queue implies adding it to the end.

Solution 3 - Python

Because it appends an element to a list? Push is usually used when referring to stacks.

Solution 4 - Python

Because "append" intuitively means "add at the end of the list". If it was called "push", then it would be unclear whether we're adding stuff at the tail or at head of the list.

Solution 5 - Python

Not an official answer by any means (just a guess based on using the language), but Python allows you to use lists as stacks (e.g., section 5.1.1 of the tutorial). However, a list is still first of all a list, so the operations that are common to both use list terms (i.e., append) rather than stack terms (i.e., push). Since a pop operation isn't that common in lists (though 'removeLast' could have been used), they defined a pop() but not a push().

Solution 6 - Python

FYI, it's not terribly difficult to make a list that has a push method:

>>> class StackList(list):
...     def push(self, item):
...             self.append(item)
... 
>>> x = StackList([1,2,3])
>>> x
[1, 2, 3]
>>> x.push(4)
>>> x
[1, 2, 3, 4]

A stack is a somewhat abstract datatype. The idea of "pushing" and "popping" are largely independent of how the stack is actually implemented. For example, you could theoretically implement a stack like this (although I don't know why you would):

l = [1,2,3]
l.insert(0, 1)
l.pop(0)

...and I haven't gotten into using linked lists to implement a stack.

Solution 7 - Python

Ok, personal opinion here, but Append and Prepend imply precise positions in a set.

Push and Pop are really concepts that can be applied to either end of a set... Just as long as you're consistent... For some reason, to me, Push() seems like it should apply to the front of a set...

Solution 8 - Python

Push is a defined http://en.wikipedia.org/wiki/Stack_%28data_structure%29">stack</a> behaviour; if you pushed A on to stack (B,C,D) you would get (A,B,C,D).

If you used python append, the resulting dataset would look like (B,C,D,A)

Edit: Wow, holy pedantry.

I would assume that it would be clear from my example which part of the list is the top, and which part is the bottom. Assuming that most of us here read from left to right, the first element of any list is always going to be on the left.

Solution 9 - Python

Probably because the original version of Python (CPython) was written in C, not C++.

The idea that a list is formed by pushing things onto the back of something is probably not as well-known as the thought of appending them.

Solution 10 - Python

From PEP 20 -- The Zen of Python:

> There should be one-- and preferably only one --obvious way to do it.

Having both list.append and list.push would be two ways of doing the same thing -- and list.append came first.

Solution 11 - Python

I am a python newbie, for myself, I use this:

def push(one, array):
    array.append(one)
def pop(array):
    if len(array) > 0:
        one = array[len(array)-1]
        del array[len(array)-1]
        return one
    else:
        return None
def pop_first(array):
    if len(array) > 0:
        one = array[0]
        del array[0]
        return one
    else:
        return None

pop_first() is to add from beginning of the list (the opposite of pop())

I don't use append just to make my code more readable (from my point of view), since I also code in JS, flutter, and PHP, function name similarities is important.

Solution 12 - Python

Push and Pop make sense in terms of the metaphor of a stack of plates or trays in a cafeteria or buffet, specifically the ones in type of holder that has a spring underneath so the top plate is (more or less... in theory) in the same place no matter how many plates are under it.

If you remove a tray, the weight on the spring is a little less and the stack "pops" up a little, if you put the plate back, it "push"es the stack down. So if you think about the list as a stack and the last element as being on top, then you shouldn't have much confusion.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEddie WelkerView Question on Stackoverflow
Solution 1 - PythonAndrew DalkeView Answer on Stackoverflow
Solution 2 - PythonMatt BallView Answer on Stackoverflow
Solution 3 - PythonJesperEView Answer on Stackoverflow
Solution 4 - PythonGyomView Answer on Stackoverflow
Solution 5 - PythonUriView Answer on Stackoverflow
Solution 6 - PythonJason BakerView Answer on Stackoverflow
Solution 7 - PythondicroceView Answer on Stackoverflow
Solution 8 - PythonSatanicpuppyView Answer on Stackoverflow
Solution 9 - PythonunwindView Answer on Stackoverflow
Solution 10 - PythoncountuniqueView Answer on Stackoverflow
Solution 11 - PythonNiu BeeView Answer on Stackoverflow
Solution 12 - PythonbobView Answer on Stackoverflow