On Sat, 19 Mar 2005, Jay Loden wrote:
> How can I prepend something to a list? I thought that I could do > list.prepend() since you can do list.append() but apparently not. Any > way to add something to a list at the beginning, or do I just have to > make a new list? Hi Jay, Liam and Sean pointed out that you can either do a combination of reverse() and append(), or you can use insert(). How often are you planning to prepend onto your lists, though? The performance of both approaches can be quite different, depending on input size. Do you plan to do a lot of prepends at a time? Also, are you dealing with short lists? If you're dealing with short lists, either solution will work fine. But if your lists grow to something much larger (say, in the tens of thousands), then you might need a more specialized solution, such as a 'deque': http://www.python.org/doc/lib/module-collections.html A dequeue behaves like a list, but it also supports very fast "prepend" (in the dequeue documentation, the 'appendleft()' method), so if your needs are specialized, a deque might be an appropriate data structure for you. Best of wishes! _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor