Python | Add list at beginning of list
Sometimes, while working with Python list, we have a problem in which we need to add a complete list to other. The rear end addition of list has been discussed before. But sometimes, we need to perform an append at beginning of list. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using "+" operator
The “+” operator can be used to perform this particular task. In this, we just perform the addition of one list before other and construct a new list or perform the addition to same list.
# Python3 code to demonstrate working of # Adding list at beginning of list # using "+" operator # initialize list test_list = [1, 4, 5, 7, 6] # initialize add list add_list = [3, 4, 2, 10] # printing original list print("The original list is : " + str(test_list)) # printing add list print("The add list is : " + str(add_list)) # Adding list at beginning of list # using "+" operator test_list = add_list + test_list # printing result print("The original updated list is : " + str(test_list)) |
The original list is : [1, 4, 5, 7, 6] The add list is : [3, 4, 2, 10] The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]
Method #2 : Using deque.extendleft() + reversed()
This task can also be performed using combination of above methods. In this, we just convert the list into a dequeue, to allow a front append, and then one by one addition is done by extendleft(), the add list is reversed so that addition take place in correct order using reversed().
# Python3 code to demonstrate working of # Adding list at beginning of list # using deque.extendleft() + reversed() from collections import deque # initialize list test_list = [1, 4, 5, 7, 6] # initialize add list add_list = [3, 4, 2, 10] # printing original list print("The original list is : " + str(test_list)) # printing add list print("The add list is : " + str(add_list)) # Adding list at beginning of list # using deque.extendleft() + reversed() res = deque(test_list) res.extendleft(reversed(add_list)) # printing result print("The original updated list is : " + str(list(res))) |
The original list is : [1, 4, 5, 7, 6] The add list is : [3, 4, 2, 10] The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]
Recommended Posts:
- Python | Perform append at beginning of list
- Python | Insert the string at the beginning of all items in a list
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Pair and combine nested list to tuple list
- Python | Convert list of string into sorted list of integer
- Python | Replace elements in second list with index of same element in first list
- Python | Filter list of strings based on the substring list
- Python | Find maximum length sub-list in a nested list
- Python | Add list elements with a multi-list based on index
- Python | Sorting list of lists with similar list elements
- Python | Merge list of tuple into list by joining the strings
- Python | Merge List with common elements in a List of Lists
- Python | Convert list of numerical string to list of Integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



