tags:

views:

1846

answers:

11

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?

+12  A: 

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.

Matt Ball
The tutorial seems to suggest that it simply pushes and pops from the end: "The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index. "
Uri
"pushing" in no way means adding to the front. every implementation of a stack that has ever been written by a sane person "pushes" onto the top (end) of the stack, not the bottom (start) of the stack
Kip
*correction: every *array-based* implementation. a linked-list implementation would push to the head.
Kip
javascript `push` adds to the end.
Kobi
@Kip: You can also push a new item to the end of a linked list.
Gumbo
No, having into account `list.pop` semantics, `list.append` pushes elements into the list, when viewed as a stack.
fortran
@Gumbo you could, but the linked-list implementation is easier if you push to the front
Kip
The point that @Kip is trying to make is that "push" and "pop" don't imply any kind of ordering. It can go to the front or the back. However, append does and therefore is more precise.
Jason Baker
@Jason: I updated my answer to (hopefully) make that point more clear.
Matt Ball
I have never seen a 'push' operation which does not append the item to the end of the list.
Ed Swangren
+7  A: 

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

JesperE
A list can be a stack though. :-)
Jason Baker
A: 

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.

unwind
The second part is a good answer. But what does that have to do with being implemented in C/C++?
Jason Baker
@Jason: In C++'s STL, push_back() is how you append to a list. I was trying to convey the meta-idea that the idea that lists are formed by pushing is perhaps more likely to pop up if you're working in C++. Make any sense?
unwind
If you have a list type implemented as a contiguous array (a vector in C++, a list in Python, an array in Perl) then it makes sense to have "push" put the new element at the end. You'll please note that perl 4 supposed "push" and "pop" as functions on arrays exactly like Python's append/pop and C++'s push_back/pop_back, and well before STL was formally proposed to C++. So it has nothing to do with C++'s STL creating a new understanding of things.
Andrew Dalke
One of the things I miss learning Python from a Perl background is the ability to use built-in push(), pop(), shift(), and unshift() operations to add/remove elements to/from either end of the *same array*. Even though I can easily wrap a Python list in a "Stackish" class or a "Queueish" class, it doesn't look so easy (or efficient) to do both at once.
Peter
+5  A: 

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.

Gyom
A: 

Push is a defined stack 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.

Satanicpuppy
That's not true, pop removes from the end of the list, not from the front.
fortran
read the page you link to. push is defined as pushing onto the top of the stack. which end is the "top" depends on the implementation. in an array-based stack, push would push onto the end of the array. in a linked-list-based stack, push would push to the beginning.
Kip
+3  A: 

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().

Uri
+2  A: 

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...

dicroce
A: 

It's called "append" because "push" is a daft name for a function that appends.

A reasonable question is why "pop" is called "pop", and not "removeandreturnthelastitem". And I think that also answers itself.

Lennart Regebro
+1  A: 

Because everyone knows what "append" means. Push refers to stacks, which not everyone (not even every programmer!) understands.

Kip
A: 

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.

Jason Baker
+11  A: 

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.

Andrew Dalke