How to Add Element at Beginning of List in Python?

02-Dec-2022

.

Admin

How to Add Element at Beginning of List in Python?

Hi Dev,

If you need to see example of how to add element at beginning of list in python. This post will give you simple example of add element to list python at index 0. We will use how to add an element to the start of a list in python. you'll learn how to add an element to the beginning of a list in python. follow bellow step for python add element to list beginning.

There are many ways to add elements at the first of the list in python. i will give you two examples using insert() method and list key to add element to list python at index 0. so let's see the below examples.

so let's see following examples with output:

Example 1:


main.py

myList = [1, 2, 3, 4]

# Add new element at first

myList.insert(0, "First Elem")

print(myList)

Output:

['First Elem', 1, 2, 3, 4]

Example 2:

main.py

myList = [1, 2, 3, 4]

# Add new element at firdt

myList = ["First Elem"] + myList

print(myList)

Output:

['First Elem', 1, 2, 3, 4]

#Python