The list is a built-in library of multiple functions that has several objects and is utilized for resolving large issues. On the other hand, the prepend is the operation that means adding or inserting the elements at the beginning of any Python List. If we want to add any element to the data structure, we call it an append operation.
This post will provide multiple methods for prepending a list in Python.
How to Prepend List in Python?
To prepend the Python list, the below-stated techniques are used:
- Using [ ] and + Operators
- Using Slicing Method
- Using insert() Method
- Using deque.appendleft() Method
Method 1: Prepend List in Python Using “[ ]” and “+” Operators
The simplest way to prepend the Python is by utilizing the square “[ ]” braces and “+” operator. The square braces can be used for appending values at the beginning of any list while using the “+” operator.
First, declare a list with integer values:
Then, print the list while converting it to the string:
Now, increment the list by appending specified value at the beginning of the list with square braces and the “+” operation indicates the concatenation:
Call the “print()” function with the string as an argument:
Method 2: Prepend List in Python Using Slicing Method
Another easy way to assign a desired item to the start of the list in Python is the slicing method. Let’s check out the below-given example for displaying the results of the slicing method.
Perform the slicing procedure by adding the “9” at the provided index “0”:
Print the prepended string:
Method 3: Prepend List in Python Using “insert()” Method
Use the “insert()” method to prepend a list in Python and provided by the list library. This method takes two parameters, index, and value.
Call the “insert()” function to insert the value “9” in the declared list at the “0” index:
After appending the value at the beginning of the provided list, get the results by using the print statement:
As you can see, the provided value has been added at the specified index of the list:
Method 4: Prepend List in Python Using “deque.appendleft()” Method
In Python, the “collections” module provides several data structures, and the “deque()” is one of them. The deque data structure is known as the “appendleft()” method that takes an element as a parameter and appends it at the start of the list.
First, import the “deque” data structure from the “collections” module:
Use the “deque” data structure and take list variable:
Now, append the list with a desired number at the start of the list:
Call the print statement to check the append list:
That’s all! We have elaborated on multiple ways to prepend the Python list.
To prepend the Python list, the “+” operator with square brackets “[ ]”, the “slicing” method, the “insert()“ method, and the “deque.appendleft()“ method are used. The square braces can be utilized for appending values at the beginning of any list while using the “+” operator. The “insert()” method provided by the list library takes two arguments, likewise index and value. This post demonstrated different methods for prepending a list in Python.