Binge On Code

Jun 8, 2023

589 readers

Python list - How to insert an item into a python list

Arrays otherwise known as lists in python are common data structures in python. Python list contains items and it can be modified by adding more items to it.

There are three standard ways of adding items to a python list and these are:

  • insert
  • extend
  • append

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.

1. List.insert(index, object)

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.

2. List.append(object)

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.

3. List.extend(iterable 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]

Related Articles

How to remove an item from a python list

There are a couple of ways to remove item from python list. This simple guide will focus mainly on the built in python methods for doing this!

Jun 8, 2023

Views 142

How to sort python list – part one

There is nothing as common as sorting when it comes to working with data. This tutorial will give you two part guide on how to sort python list.

Jun 8, 2023

Views 156

How to sort python list – part two

This is the second part of this awesome short series of how to sort python list. We will be sorting a list of python dictionaries! So let’s do this!

Jun 8, 2023

Views 275

Angular JavaScript React CSS Django Python React Native Next JS State Management Django Rest Framework Unity