GeeksforGeeks App
Open App
Browser
Continue

# Python | Perform append at beginning of list

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.

## Python3

 `# Python3 code to demonstrate``# to add element at beginning``# using insert()` `# initializing list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``7``]` `# printing initial list``print` `("Original ``list` `: " ``+` `str``(test_list))` `# using insert() to append``# at beginning. append 6``test_list.insert(``0``, ``6``)``         ` `# printing resultant 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]```

Time Complexity: O(n), where n is the number of elements in the list. This is because the insert method has to shift all the elements of the list to accommodate the newly inserted element, which takes O(n) time.
Auxiliary Space: O(1), as the insert method does not require any additional memory to be allocated.

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.

## Python3

 `# Python3 code to demonstrate``# to add element at beginning``# using [] and +` `# initializing list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``7``]` `# printing initial list``print` `("Original ``list` `: " ``+` `str``(test_list))` `# using [] and + to append``# at beginning append 6``test_list ``=` `[``6``] ``+` `test_list``         ` `# printing resultant 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]```

Time complexity: O(n)
Auxiliary space: O(n) where n is the length of the list.

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.

## Python3

 `# Python3 code to demonstrate``# to add element at beginning``# using slicing` `# initializing list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``7``]` `# printing initial list``print` `("Original ``list` `: " ``+` `str``(test_list))` `# using slicing to append``# at beginning. append 6``test_list[:``0``] ``=` `[``6``]``         ` `# printing resultant 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]```

Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(1), as no additional space is used to store the elements.

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.

## Python3

 `# Python3 code to demonstrate``# to add element at beginning``# using collections.deque.pushleft()``from` `collections ``import` `deque` `# initializing list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``7``]` `# printing initial list``print` `("Original ``list` `: " ``+` `str``(test_list))` `# using collections.deque.pushleft()``# to append at beginning``# append 6``test_list ``=` `deque(test_list)``test_list.appendleft(``6``)``test_list ``=` `list``(test_list)``         ` `# printing resultant 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]```

Time complexity: O(1)
Auxiliary space: O(1)

Method #5 : Using extend() method

## Python3

 `# Python3 code to demonstrate``# to add element at beginning` `# initializing list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``7``]` `# printing initial list``print` `(``"Original list : "` `+` `str``(test_list))``x``=``[``6``]``x.extend(test_list)``        ` `# printing resultant list``print` `(``"Resultant list is : "` `+` `str``(x))`

Output

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

Time Complexity: O(n), where n is the length of the original list.
Auxiliary Space: O(n), as a new list is created which is equal in length to the original list.

Method 6: Using list() and itertools.chain() functions.

Algorithm:

1. Initialize a list test_list.
2. Print the original list.
3. Use itertools.chain() function to concatenate the given list with a list containing the element to be added at the beginning.
4. Convert the resulting iterable into a list using list() function.
5. Print the resulting list.

## Python3

 `# Python3 code to demonstrate``# to add element at beginning``# using itertools``import` `itertools` `# initializing list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``7``]` `# printing initial list``print` `(``"Original list : "` `+` `str``(test_list))` `# using itertools to append``# at beginning. append 6``test_list ``=` `list``(itertools.chain([``6``], test_list))``        ` `# printing resultant 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]```

Time Complexity: O(n), where n is the length of the original list, because the entire list has to be copied to a new list with the additional element at the beginning.

Auxiliary Space: O(n+1), where n is the length of the original list, because a new list is created to store the modified list.

#### Using numpy.concatenate() function:

Approach:

1. Convert the input list into a numpy array.
2. Create a numpy array of the element to be added at the beginning of the list.
3. Concatenate both arrays along the first dimension.
4. Convert the concatenated array back to a list.
5. Print the resultant list.

## Python3

 `import` `numpy as np` `# initializing list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``7``]` `# printing initial list``print``(``"Original list : "` `+` `str``(test_list))` `# element to be added``element ``=` `6` `# convert the input list to a numpy array``arr ``=` `np.array(test_list)` `# create a numpy array of the element to be added``arr_elem ``=` `np.array([element])` `# concatenate both arrays along the first dimension``concatenated_arr ``=` `np.concatenate((arr_elem, arr), axis``=``0``)` `# convert the concatenated array back to a list``result_list ``=` `concatenated_arr.tolist()` `# printing resultant list``print``(``"Resultant list is : "` `+` `str``(result_list))`

Output:

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

Time Complexity: O(n), where n is the number of elements in the list. The time complexity is dominated by the conversion of the list to a numpy array and back to a list.
Auxiliary Space: O(n), as we are creating a numpy array to store the element to be added.

Using reduce():

1. Import the reduce() function from the functools module.
2. Initialize the input list.
3. Use reduce() function to concatenate two lists with the element to be added at the beginning.
a. The first argument of the reduce function is a lambda function which takes two lists as input and returns their concatenation.
b. The second argument is a list containing an empty list and the input list.
c. The lambda function concatenates the two lists, placing the first list (containing the element to be added) at the beginning of the result.
4. Assign the result of reduce() to the input list.
5. Print the updated list.

## Python3

 `from` `functools ``import` `reduce` `# Initializing list``test_list ``=` `[``1``, ``3``, ``4``, ``5``, ``7``]` `# Printing initial list``print``(``"Original list: "``, test_list)` `# Using reduce() function to insert an element at the beginning of the list``test_list ``=` `reduce``(``lambda` `x, y: [``6``] ``+` `x ``+` `y, [[], test_list])` `# Printing the resultant list``print``(``"Resultant list: "``, test_list)``#This code is contributed by Rayudu.`

Output

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

Time Complexity:
– The `reduce()` function has a time complexity of O(n), where n is the number of elements in the list. This is because `reduce()` applies the lambda function to each pair of elements in the list until a single result is obtained.
– In our case, we are concatenating two lists containing a total of n elements, so the time complexity is O(n).

Auxiliary Space:
– The space complexity of the `reduce()` function depends on the size of the input list and the size of the lambda function.
– In our case, we are using a lambda function that concatenates two lists. Since the size of the input list and the size of the lambda function are both O(n), the space complexity is O(n). This is because the lambda function creates a new list in memory for each pair of lists that it concatenates, so the space required is proportional to the size of the input list.

My Personal Notes arrow_drop_up