. Advertisement .
..3..
. Advertisement .
..4..
Many situations don’t require you to change the whole list. You may need to only append at the front of a list in Python instead of removing an item, for instance. But this task can be quite difficult without the necessary knowledge.
Check these two methods to learn more about this list manipulation task.
How To Append At The Front Of A List In Python
Using list.insert()
The list object in Python comes with many built-in methods, some of which can modify the items of a list. In particular, list() can insert an element into a specified position.
Syntax:
list_obj.insert(idx, elem)
The syntax of this method is quite simple, with two required parameters.
It will insert element elem into index idx of the list_obj list while shifting affected elements to achieve the result. Every element located after the idx position will be moved to the right to make room for the new element.
Changes are performed in the current list, and the insert() method returns None.
If you want to insert a new item at the beginning of the list, use the 0 indexes.
The reason is Python counts from zero when indexing a list (zero-based indexing). The first item always has the index of 0, the second – 1, and so on.
You can also use negative indexes. For instance, -1 points to the item at the end of the list.
This is an example of using this insert() method:
Code:
countries = ['Hungary', 'Israel', 'USA', 'France', 'Germany', 'Russia', 'Italy']
countries.insert(1, 'Brazil')
print(countries)
Output:
['Hungary', 'Brazil', 'Israel', 'USA', 'France', 'Germany', 'Russia', 'Italy']
Concatenation
List concatenation allows you to join two lists. It means you can add a second list to the beginning of the original list.
The easier to perform this operation is to use the plus (+) operator. Unlike inset(), it doesn’t modify its operands. The + operator creates a new list (the resultant list) while two original lists stay intact.
This is how you can use the plus operator to replace inset() in the previous example.
Code:
fruits = ['Coconut', 'Apple', 'Orange', 'Pear', 'Strawberry', 'Jackfruit', 'Durian']
extra_fruit = ['Cherry']
concatenation1 = extra_fruit + fruits
print(concatenation1)
Output:
['Cherry', 'Coconut', 'Apple', 'Orange', 'Pear', 'Strawberry', 'Jackfruit', 'Durian']
Keep in mind that the plus operator only accepts two objects of the same type. Therefore, you must provide the new items in the form of a list.
You can use this to append several items to the beginning of a list at once as well.
Code:
animals = ['Lion', 'Hyena', 'Rhinoceros', 'Beaver']
extra_animals = ['Elephant', 'Gnu']
concatenation2 = extra_animals + animals
print(concatenation2)
Output:
['Elephant', 'Gnu', 'Lion', 'Hyena', 'Rhinoceros', 'Beaver']
This can’t be done with list.insert(), which can only insert a new element at a time. You will get unwanted results when trying the same thing with insert().
Code:
animals = ['Lion', 'Hyena', 'Rhinoceros', 'Beaver']
extra_animals = ['Elephant', 'Gnu']
animals.insert(1, extra_animals)
print(animals)
Output:
['Lion', ['Elephant', 'Gnu'], 'Hyena', 'Rhinoceros', 'Beaver']
The new list will be added to the beginning of the first list, but as a list object. If you insist on using list.insert(), try to loop through the list containing the added items.
Code:
animals = ['Lion', 'Hyena', 'Rhinoceros', 'Beaver']
extra_animals = ['Elephant', 'Gnu']
for a in reversed(extra_animals):
animals.insert(1, a)
print(animals)
Output:
['Lion', 'Elephant', 'Gnu', 'Hyena', 'Rhinoceros', 'Beaver']
We need to use the reversed() function and go through the new list in the opposite direction. Otherwise, you will add items in the wrong order.
Code:
animals = ['Lion', 'Hyena', 'Rhinoceros', 'Beaver']
extra_animals1 = ['Elephant', 'Gnu', 'Tiger']
for a in extra_animals1:
animals.insert(1, a)
print(animals)
Output:
['Lion', 'Tiger', 'Gnu', 'Elephant', 'Hyena', 'Rhinoceros', 'Beaver']
Conclusion
While there may be more advanced ways to append at the front of a list in Python, those are the simplest methods. Using list.insert() and concatenation with + should be enough for most programming purposes.
Leave a comment