Python list insert

In this article, you’ll see how to insert an element into a list in Python. The insert method is used to add an element to a list at any position. The append method takes two arguments. The first argument is the index, and the second argument is the element that can be an integer, float, or any other data type such as a list. I highly recommend you get the “Python Crash Course Book” to learn Python.

Insert an integer in a list

a = [10, 15, 25, 30, 40]
a.insert(1, 20)
print(a)

Output:

[10, 20, 15, 25, 30, 40]

Add a list as an element

a = [10, 15, 25, 30, 40]
a.insert(1, [10,20])
print(a)

Output:

[10, [10, 20], 15, 25, 30, 40]

Leave a Comment

Your email address will not be published.