insert()to Append an Element to the Front of a List in Python
+Operator to Append an Element to the Front of a List in Python
- Use Unpacking to Insert an Element Into the Beginning of a List
This tutorial will demonstrate different ways on how to append an element to the front of a list in Python.
Throughout the tutorial, a list of integers will be used as examples to focus on list insertion instead of inserting various data types since the list insertion approach should be the same regardless of what data type the list contains.
insert() to Append an Element to the Front of a List in Python
insert() function inserts an element to the given index of an existing list. It accepts two parameters, the index to be inserted into and the value to insert.
For example, we’ll insert an element into an existing list of size
5. To append an element to the front of the list using this function, we should set the first argument as
0, which denotes that the insertion is done at index
0 - the beginning of the list.
int_list = [13, 56, 5, 78, 100] int_list.insert(0, 24) print(int_list)
[24, 13, 56, 5, 78, 100]
+ Operator to Append an Element to the Front of a List in Python
Another approach to append an element to the front of a list is to use the
+ operator. Using the
+ operator on two or more lists combines them in the specified order.
If you add
list1 + list2 together, then it concatenates all the elements from
list2 after the last element of
list1. For example, let’s add a single integer into the beginning of an already existing list using the
to_insert = 56 int_list = [13, 5, 78, 19, 66] int_list = [to_insert] + int_list print(int_list)
to_insert variable is encapsulated with square brackets
. This is done to convert the single integer into the list data type to make list addition possible.
[56, 13, 5, 78, 19, 66]
Use Unpacking to Insert an Element Into the Beginning of a List
Unpacking is an operation in Python that allows unique iterable manipulations to be possible. Unpacking allows iterable assignment to be more flexible and efficient for the developers.
Unpacking also allows merging existing iterables, which is the operation that will be used to insert into the beginning of the list for this example.
To append an element to the beginning of a list using unpacking, we use the unpacking operator
* to merge the single integer and the existing list, placing the integer at the beginning of the newly formed list.
to_insert = 7 int_list = [19, 22, 40, 1, 78] int_list = [to_insert, *int_list] print(int_list)
[7, 19, 22, 40, 1, 78]
Performance-wise, using unpacking is the fastest out of all the solutions mentioned. The
insert() method is a close second to unpacking. Using the
+ operator is significantly slower than both the solutions mentioned above.
If you’re inserting into the beginning of a list with a significant number of elements, it’s best to use either unpacking or
insert() for faster runtime.
- Convert a Dictionary to a List in Python
- Remove All the Occurrences of an Element From a List in Python
- Remove Duplicates From List in Python
- Get the Average of a List in Python
- What Is the Difference Between List Methods Append and Extend
- Convert a List to String in Python