Python Programming Language: How to Add Elements Into a List

Jesse L
2 min readMay 12, 2022

Hi everyone, welcome back. In these examples, we will be going over how to add elements into a List in Python. Lists can be used to store a collection of data. A list in Python is mutable and can allow duplicate values. The elements with in a list are indexed and ordered. With this introduction out of the way, let’s get into it.

Creating List

Let’s start by creating our List:

myList = []

Now, we have created an empty list by using the square brackets, but let’s see how we can populate it on creation:

myList = [4, 27, 58]

So now, we have created a List with a few elements. As we can see in this example, we have used the integer data type. We can store data of all sorts of types within a List, such as the String data type. Now that we have our List created, let’s see how we can add elements into it.

Adding Elements Into The List

Now, let’s try to add elements into our List. We can do this by using the built in append() function that is provided. Let’s see an example:

myList = [4, 27, 58]
myList.append(83)
print(myList)
Output:
[4, 27, 58, 83]

In the example above, we can see that we added the number “83” into our List by using the append() function. We can verify that it was successfully added by printing our List and viewing the output. The append() function will add an element to the end of a list.

Let’s see how we can add an element into the middle of the list. We can do that by using the insert() function. With the insert() function, we will need to specify the index of the new element. Let’s see an example:

myList = [4, 27, 58]
myList.insert(1, 83)
print(myList)
Output:
[4, 83, 27, 58]

In the example above, we can see that we added the number “83” at the index of “1” by using the insert() function. We can verify that it was successfully added and placed in the correct position by printing our List and checking the output.

Conclusion

That is it for the adding elements into a Python List examples. We covered how to add elements into a List by using the append() and insert() function. I hope this helps. If there are any questions or comments, please let me know. Thanks for reading.

Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.