How to prepend to a list in Python? A list is a built-in data structure that allows you to store and manipulate collections of elements. It provides several built-in functions and techniques for performing operations on lists. The operation of adding or inserting elements at the beginning of a list is commonly referred to as the prepend operation. It involves placing elements at the front or the beginning of the list. This operation is useful when you want to add elements to the list in a way that shifts the existing elements to higher indices.
You can prepend an element or multiple elements to a list in Python, you can use various methods. For example, by using insert(), [] notation & + operator, slicing, and deque.appendleft() functions. In this article, I will explain how to prepend a list by using all these functions with examples.
1. Quick Examples of Prepending a List
If you are in a hurry, below are some quick examples of how to prepend to a list.
# Quick examples of list prepend
# Initialize list
mylist = [“Python”,”Hadoop”,”Spark”]
# Example 1: Prepend to a list
# Using insert() method
mylist.insert(0,”java”)
# Example 2: Prepend to a list
# Using the [] and + operators
element = “MongoDB”
mylist = [element] + mylist
# Example 4: Prepend to a list
# Using the slicing method
element = “MongoDB”
new_list = [element] + mylist[:]
# Example 5: Using the slicing method
element = “MongoDB”
mylist[:0] = [element]
# Example 6: Using deque.appendleft() function
element = “MongoDB”
deque_list = deque(mylist)
deque_list.appendleft(element)
new_list = list(deque_list)
# Example 7: Using list comprehension
new_list = [“MongoDB”] + [item for item in mylist]
2. Prepend to a List Using insert() Method
You can prepend an element to a list using the insert() method. In this program, the initial list mylist contains three elements: “Python“, “Hadoop“, and “Spark“. Then, the insert() method is used to prepend the element “java” at index 0, which shifts the existing elements to higher indices. After prepending operation, the list becomes [‘java’,’Python’,’Hadoop’,’Spark’].
# Initialize list
mylist = [“Python”,”Hadoop”,”Spark”]
print(“Actual List: “,mylist)
# Prepend to a list
# Using insert() method
mylist.insert(0,”java”)
print(“After prepending the list:”, mylist)
Yields below output.
3. Prepend a List Using the [ ] and + Operators
You can use the square bracket [] notation and the + operator to prepend an element to a list. For example, the element “MongoDB” is stored in the variable element. By using the concatenation operator +, you can combine a new list containing an element with the existing mylist. The resulting list is then assigned back to mylist, effectively prepending the element.
# Initialize list
mylist = [“Python”,”Hadoop”,”Spark”]
print(“Actual List: “,mylist)
# Prepend to a list
# Using the [] and + operators
element = “MongoDB”
mylist = [element] + mylist
print(“After prepending list:”, mylist)
Yields below output.
4. Prepend a List Using the Slicing Technique
You can also use the slicing technique to prepend a list in Python. First, you can create a new list by concatenating the element you want to prepend with the existing list.
In the below example, the element “MongoDB” is stored in the variable element. The slicing operator mylist[:] creates a shallow copy of the existing list mylist. By using the concatenation operator +, you can combine a new list containing elements with the shallow copy of mylist. The resulting list is assigned to new_list, effectively prepending the element.
# Initialize list
mylist = [“Python”,”Hadoop”,”Spark”]
print(“Actual List: “,mylist)
# Prepend to a list
# Using the slicing technique
element = “MongoDB”
new_list = [element] + mylist[:]
print(“After prepend list:”, new_list)
# Using the slicing method
element = “MongoDB”
mylist[:0] = [element]
print(“After prepending the list:”, mylist)
Yields the same output as above.
5. Prepend a List Using collections.deque.appendleft() Method
You can prepend an element to a list using the appendleft() method from the collections.deque class. Before going to use the deque class you need to convert your list into a deque object.
For example, the deque() function is used to convert the list mylist into a deque object named deque_list. Then, the appendleft() method is used to prepend the element “MongoDB” to the deque object. Finally, the updated deque object is converted back to a list using the list() function, resulting in [‘MongoDB’, ‘Python’, ‘Hadoop’, ‘Spark’].
from collections import deque
mylist = [“Python”, “Hadoop”, “Spark”]
print(“Actual List: “,mylist)
# Using deque.appendleft() function
element = “MongoDB”
deque_list = deque(mylist)
deque_list.appendleft(element)
new_list = list(deque_list)
print(“After prepending the list:”, new_list)
Yields the same output as above.
6. Prepend a List Using List Comprehension
Using list comprehension, you can prepend the element “MongoDB” to the list. For example, the initial list mylist contains three elements: “Python“, “Hadoop“, and “Spark“. The list comprehension [item for item in mylist] iterates over each item in mylist and creates a new list containing the same elements. By using the + operator, the element “MongoDB” is concatenated with the new list, effectively prepending it.
# Initialize list
mylist = [“Python”, “Hadoop”, “Spark”]
print(“Actual List: “,mylist)
# Using list comprehension
new_list = [“MongoDB”] + [item for item in mylist]
print(“After prepending the list:”, new_list)
Yields the same output as above.
Conclusion
In this article, I have explained how to prepend a list in Python by using insert(), [] notation & + operator, slicing, and collections.deque.appendleft() functions with examples.
Happy Learning !!
How to prepend to a list in Python? A list is a built-in data structure that allows you to store and manipulate collections of elements. It provides several built-in functions and techniques for performing operations on lists. The operation of adding or inserting elements at the beginning of a list is commonly referred to as Read More Python, Python Tutorial, Pending review by admin, Python List Examples