Methods for Python Prepend to List

Do you ever wonder why there is a missing function named prepend() instead Python lists support functions like append() and extend()?
This is because inserting any item at the beginning of the list is expensive as every item that made up the list needs to be shifted right by one space i.e. more disruptive to the existing data in the list.

Thus in this sense Python doesn’t provide a list.prepend() due to the performance issues for the large lists. So we have to manage this case ourselves by utilizing the best method according to our use cases.

 

Python Prepend to List

The four methods that we are going to discuss for prepend to list in Python are given as below:

Method 1: Using List insert() method

The list insert() method is applicable when you want to insert an item at any position on the list. It is the built-in method that we can use to insert an item at the beginning by mentioning the index position as 0.

It requires passing two arguments: index position and value. The time complexity of this method is O(n) which is linear. Overall the efficiency depends on the size of the lists.

Syntax:

insert(index, value)

Let’s take an example of a list that contains the working days in a week that first starts with Monday.

days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"]

Let’s suppose you want to add “Sat” at the start of the list. In this case insert method comes in handy and efficient as the list size is short in size and shifting the item to the right doesn’t affect the overall performance of your system.

>>> days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> days_list.insert(0, "Sun")
>>> days_list
['Sun', 'Mon', 'Tue', 'Wed', 'Thurs', 'Fri']

Here the index value should be 0 since it denotes the start of the list.

For inserting an item at the end into the list, you can simply use the list append() method as:

>>> days_list.append("Sat")
>>> days_list
['Mon', 'Tue', 'Wed', 'Thurs', 'Fri', 'Sat']

 

Method 2: Using Collections module: DeQue

The DeQue package provided by the Python built-in collections module is similar to the double-ended queue where we can insert items from both ends either from the start or end of the list.

The time complexity is O(1) which is constant. This method doesn’t depend on the size of the list, thus having constant performance while adding items to the list.

To use this, first, we have to convert the list object into a deque object and use the appendleft() method in order to insert an item at the beginning of the list.

>>> from collections import deque
>>> days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> days_deque = deque(days_list)
>>> days_deque
deque(['Mon', 'Tue', 'Wed', 'Thurs', 'Fri'])
>>> days_deque.appendleft("Sun")
>>> days_deque
deque(['Sun', 'Mon', 'Tue', 'Wed', 'Thurs', 'Fri'])

 

To convert the deque object back to a list, you type cast the object into a list as:

>>> list(days_deque)
['Sun', 'Mon', 'Tue', 'Wed', 'Thurs', 'Fri']
<class 'collections.deque'>
>>> type(list(days_deque))
<class 'list'>

 

Method 3: Using + Operator

Prepending to list using the + operator is quite easy as you don’t need to remember any built-in Python functions.

Insert item at the start

>>> days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> days_list = ["Sun"] + ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> days_list
['Sun', 'Mon', 'Tue', 'Wed', 'Thurs', 'Fri']

 

Insert item at the end

>>> days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"] + ["Sat"]
>>> days_list
['Mon', 'Tue', 'Wed', 'Thurs', 'Fri', 'Sat']

Note: When you are using this method, you are not appending an item to the actual list but instead you are creating the new list on the top of the previous list in order to insert the item.

Example:
Object id of the days_list changed after we insert the item as:

>>> days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> id(days_list)
2194821123008
>>> days_list = ["Sun"] + ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> id(days_list)
2194822193472

You can see the difference in the object id after using the + operator.

Overall, this approach is not recommended as it points out the new object id that leads to more memory used while executing the program.

For short list items, it doesn’t use that amount of memory but if you are using it for a list that contains millions of items that could be the performance issue.

 

Method 4: Using the List Slicing technique

This method of prepending to list in Python is also similar to the insert() method that we discussed at the first. Here we use a colon(:) to insert the item at the beginning or at any position of the list as:

>>> days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> days_list[:0] = ["Sun"]
>>> days_list
['Sun', 'Mon', 'Tue', 'Wed', 'Thurs', 'Fri']

Insert a new item after “Mon” and discard “Sun” & “Mon” using the slicing technique as:

>>> days_list = ["Mon", "Tue", "Wed", "Thurs", "Fri"]
>>> days_list[:1] = ["Holiday"]
>>> days_list
['Holiday', 'Tue', 'Wed', 'Thurs', 'Fri']

 

Conclusion

So far we have discussed four methods to insert elements at the beginning of the list.

In terms of computation time, prepending the list using the + operator is less efficient than using insert() and slicing methods. Because creating a new list would be less efficient as all of the object reference counts would have to be updated.

If the size of the list is short, we can use the insert() or slicing methods but if the list size is large we have to use the deQue package provided by the collections module as it is efficient in computing and takes constant time O(1) to insert the item both sides irrespective of the size.

If you have any confusion, feel free to drop down comments right below.

Happy Coding 🙂

Learn more about Python Lists and its methods right here.

Leave a Comment