List prepend python

The usual append operation of Python list adds the new element at the end of the list. But in certain situations, we need to append each element we add in front of list. If we perform brute force techniques, we need to perform unnecessary shifts of elements and hence, having shorthands for it is useful.

Let’s discuss certain ways to perform append at beginning of the list.

Method #1 : Using insert()

This method generally inserts the element at any position in the list and also performs the necessary shifts required internally and hence can also be used to perform this very task.

test_list = [1, 3, 4, 5, 7]

print ("Original list : " + str(test_list))

test_list.insert(0, 6)

print ("Resultant list is : " + str(test_list))

Output:

Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]

 
Method #2 : Using [] and +

These both operators can be combined to perform this task. We convert the element to list and then perform the list addition.

test_list = [1, 3, 4, 5, 7]

print ("Original list : " + str(test_list))

test_list = [6] + test_list

print ("Resultant list is : " + str(test_list))

Output: Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]

 
Method #3 : Using Slicing

Slicing of list is also another method to perform this particular task. we just assign to the 0 sliced list to the list converted from the element. This does the trick and is quite elegant.

test_list = [1, 3, 4, 5, 7]

print ("Original list : " + str(test_list))

test_list[:0] = [6]

print ("Resultant list is : " + str(test_list))

Output: Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]

 
Method #4 : Using collections.deque.appendleft()

The list can be converted to deque and then the appendleft() can be used to perform the push like operation from the front of the doubly ended queue.

from collections import deque

test_list = [1, 3, 4, 5, 7]

print ("Original list : " + str(test_list))

test_list = deque(test_list)

test_list.appendleft(6)

test_list = list(test_list)

print ("Resultant list is : " + str(test_list))

Output: Original list : [1, 3, 4, 5, 7] Resultant list is : [6, 1, 3, 4, 5, 7]


Article Tags :

Practice Tags :

Appending or prepending a value to a list in Python.

Append value

a = ["v1", "v2", "v3", "v4"] print a a.append("new val") print a['v1', 'v2', 'v3', 'v4'] ['v1', 'v2', 'v3', 'v4', 'new val']

Env: Python 2.7.16

Prepend value

a = ["v1", "v2", "v3", "v4"] print a a.insert(0, "new val") print a a = ["new value2"] + a print a['v1', 'v2', 'v3', 'v4'] ['new val', 'v1', 'v2', 'v3', 'v4'] ['new value2', 'new val', 'v1', 'v2', 'v3', 'v4']

Env: Python 2.7.16

Suggested posts:

Share this article:

Usually, we use the append() operation to append any item at the end of the list. However, in some cases, it is required to append an element at the beginning of a list which is known as prepending an element to a list. In this tutorial, we will learn about how to prepend to a list in Python. We will learn different techniques along with examples to get a better understanding.

If you want to learn more about lists, See Python List Tutorials

Prepend to a list Using Slicing Operation

The slicing operation can be used to perform this particular task. In this method, we assign the desired item to the 0th slice of the list. For example

# initializing list x = ['b',0.5, 1,'d', 22] # using slicing method to append at beginning x[:0] = ['a'] # printing list print(x)

Output:

['a', 'b', 0.5, 1, 'd', 22]

In this example, using the slicing technique, we appended an item ‘a’ at the beginning of the list at the 0th index.

prepend to a list using list insert() function

One other possible solution is to use the insert() function. The insert function takes two parameters as an input which are the index and the item that you want to insert. On running the code, it inserts that item at the specified index. For example

# initializing list x = [8, 2, 9, 10, 7] # using insert() to append at beginning x.insert(0, 4) # printing list print(x)

Output:

[4, 8, 2, 9, 10, 7]

If you want to print the list without square brackets, check this tutorial. This method is only efficient for small lists. In order to insert an item in the beginning, we need to move down all the elements by one. Therefore, this method is inefficient for large lists.

prepend to a string using ‘+’ operator in python

Another easiest solution is to create another list consisting of the desired item that you want to prepend at the first index. Then combine the two lists using the ‘+’ operator. The following code snippet shows the implementation.

# initializing list x = [8, 2, 9, 10, 7] # using '+' operator to append at beginning x = [4] + x # printing list print(x)

Output:

[4, 8, 2, 9, 10, 7]

You can also add a character using the ‘+’ operator. Let’s see another example demonstrating the insertion of a character in a list of strings.

# initializing list x = ['b','c','d','e'] # using '+' operator to append at beginning x = ['a'] + x # printing list print(x)

Output:

['a', 'b', 'c', 'd', 'e']

In short, in this article, we have learned the three different methods by which we can add any item at the beginning of a list. If you have any queries or you want us to cover any specific topic, please let us know in the comments. Your suggestions would be highly appreciated. Contact us. See more Python Tutorials