Member-only story
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]