| by Arround The Web | No comments

Python Prepend List

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:

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.

Example

First, declare a list with integer values:

myList = [6, 8, 3, 5, 7, 1, 2]

Then, print the list while converting it to the string:

print("Original List: " + str(myList))

Now, increment the list by appending specified value at the beginning of the list with square braces and the “+” operation indicates the concatenation:

new_list = [9] + myList

Call the “print()” function with the string as an argument:

print("After Prepend List: " + str(new_list))

Output

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.

Example

Perform the slicing procedure by adding the “9” at the provided index “0”:

myList[:0] = [9]

Print the prepended string:

print("After Prepend List: " + str(myList))

Output

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.

Example

Call the “insert()” function to insert the value “9” in the declared list at the “0” index:

myList.insert(0, 9)

After appending the value at the beginning of the provided list, get the results by using the print statement:

print("After Prepend List: " + str(myList))

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.

Example

First, import the “deque” data structure from the “collections” module:

from collections import deque

Use the “deque” data structure and take list variable:

myList = deque(myList)

Now, append the list with a desired number at the start of the list:

myList.appendleft(9)

Call the print statement to check the append list:

print("After Prepend List: " + str(myList))

Output

That’s all! We have elaborated on multiple ways to prepend the Python list.

Conclusion

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.

Share Button

Source: linuxhint.com

Leave a Reply