There are three standard ways of adding items to a python list and these are:
Of course, many more variations may exist for adding items into a python list, but for now, we focus on the above main methods.
So as an example of how to add item to python list, we will use simple list of numbers. Even though a python list can have items of different types, it is mostly advisable to have a list contain related types only.
We will use an imaginary list of numbers, called covered_topics_ids. This we can assume is a list of ids which dictate the topics in python which you have already covered.
covered_topics_ids = []
NB: A pro tip to take note of is that these three methods modify the actual list and return None. So, you should not assign the original list to the return values of the methods.
This is the first method you can use to insert item into python list. It accepts the index and the object you want to insert. The index starts at 0 and the object can be of any type, as mentioned above.
Now that you have an understanding of how to add item to python list using .insert method, we can add it to our list of covered_topics_ids.
TIP: notice that we need an index, and the id. Here, we will add this item as the first one.
covered_topics_ids = []
covered_topics_ids.insert(0, 1234)
print(covered_topics_ids)
Now when you print out your list of covered topics you should see the 1234 as the first in your list, so it is the most recent topic you covered.
For example you try something list this.
covered_topics_ids = []
covered_topics_ids.insert(0, 1234)
print(covered_topics_ids)
covered_topics_ids.insert(10, 123410)
print(covered_topics_ids)
Now when you print out your list, you will see:
[1234, 123410]
Well, the item will be inserted at the end of the list - another cool way of using .insert to add item to the end of the list, huh! BUT this is not the best, the best option of add item to python list at the end, the best option is the next one.
This is the other method you can use to add item to python list. It only accepts one parameter, the item to add.
So, this is the second tip that you learnt about how to insert item into python list. You are happy with it, and like a pro, you decide to add it to the end of the list, and thanks to .append method, boom you are done in a one liner!
covered_topics_ids = []
covered_topics_ids.insert(0, 1234)
print(covered_topics_ids)
covered_topics_ids.insert(10, 123410)
print(covered_topics_ids)
covered_topics_ids.append(12345)
print(covered_topics_ids)
Now, when you print out your list of covered topics, 12345 should appear as the last one in the list.
The first two methods above dealt with a single object being added into a python list. So, this approach to insert item into python list is a bit different as it deals with an iterable list.
So, let's say that over time you were not keeping track of all the skills you learnt in python.
You finally decide to add them to your list of covered topics. That is where .extend comes into the picture.
covered_topics_ids = []
covered_topics_ids.insert(0, 1234)
print(covered_topics_ids)
covered_topics_ids.insert(10, 123410)
print(covered_topics_ids)
covered_topics_ids.append(12345)
print(covered_topics_ids)
overdue_updated_topics = [123456, 1234567]
covered_topics_ids.extend(overdue_updated_topics)
print(covered_topics_ids)
Now when you print out your list of covered topics, you will see:
[1234, 12345, 123456, 1234567]