Add Element to a List in Python Example

25-Nov-2022

.

Admin

Add Element to a List in Python Example

Hi Dev,

Today, I will let you know example of add element to a list in python example. we will help you to give example of python list insert at index. Here you will learn python add list to list at index. we will help you to give example of python insert list into list at index. you will do the following things for how to add elements in list in python.

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

so let's see following examples with output:

Example 1:


main.py

myList = ["One", "Two", "Three"]

# Adding new Element to List

myList.append("Four")

print(myList)

Output:

['One', 'Two', 'Three', 'Four']

Example 2:

main.py

myList = ["One", "Two", "Three"]

# Adding new Element to List

myList.insert(1, "Four")

print(myList)

Output:

['One', 'Four', 'Two', 'Three']

#Python