Python list - Insert the object at the first position

You can insert the element at the first position in the list.

a = ['Apple', 'Microsoft', 'Amazon', 'Google', 'Facebook', 'NVIDIA']

a.insert(0, 'Tesla')

print(a)  # ['Tesla', 'Apple', 'Microsoft', 'Amazon', 'Google', 'Facebook', 'NVIDIA']

The insert() inserts the object to the list and the first argument is the inserted index. If you want to insert at the last position, use the append method.

Use of collections.deque

from collections import deque

a = ['Apple', 'Microsoft', 'Amazon', 'Google', 'Facebook', 'NVIDIA']
d = deque(a)

d.appendleft('Tesla')

print(d)  # deque(['Tesla', 'Apple', 'Microsoft', 'Amazon', 'Google', 'Facebook', 'NVIDIA'])

for i in d:
    print(i)

# Tesla
# Apple
# Microsoft
# Amazon
# Google
# Facebook
# NVIDIA

The deque object has the appendleft() method that is similar to insert(0, x).

Python List

Python Tutorial