Search

Python Functions That Add Items To Python Lists And Their Applications

In an earlier post, I discussed on how to remove items from python lists. Today, we will expand on the concept of lists and python list functions. We will be discussing on how to add items to python lists. In doing this, we are going to be using two functions, python’s append to list and python’s extend list functions. The two do the same job of adding objects to a list but they are not cousins. I mean, they are adding very different objects and use very different concepts.

python add to list
 

First we will start by discussing the python append to list function.

Python append to list function.

The syntax for this function is list.append(x). What it says is that you are going to be adding an item, x, to the end of the list. When you call the python append list function and give it an argument, x, where x is an object, it just adds x to the end of the list. Any object can be added to the end of the list, even sequences, but they are added just as one item.

Let us give examples of the python append to list function.

You can see from running the code above that no matter the length of the object being appended or the nature of the object, it is treated as a single item. So, whenever you want to add to a list and you want to treat that object as a single item in the list, you should use the python append to list function.

Python extend list function.

The syntax of the python extend list function is list.extend(iterable). The function takes an iterable and what it does is that it iterates through each of the items in the iterable and adds them to the list. It mutates the original list based on the number of items in the argument; the python extend list function is acting like a concatenating of the original list. Therefore, you could say that while in the python append to list function the length of the list increases by 1, in the python extend list function the list increases by the number of items in the iterable.

A picture is worth a thousand words. So, let’s illustrate the concept using examples.

I used the same examples for both the python append to list and python extend list functions just to help you better understand their functionality. You could see that for the cases, the python extend list function gives lists of longer length.

These two python list functions are not the only way you can add items to a python list. There are overloaded methods we could also use.

Using overloaded operators to add to python lists.

Operator overloading or function overloading is when the same built-in operator like + or * have different behaviors for different objects or classes. You might have noticed this earlier in your python journey. For example, adding integers, 2 + 3, gives a different behavior for the + operator from adding strings, ‘mango’ + ‘banana’.

We will discuss how the + and += operators are used to add iterables to lists. They are both semantically similar to the python extend list function. When using either of them, the second object must be an iterable, otherwise python will return an error.

Here is some code to show how these overloaded operators work.

So you now have at your arsenal methods to add items to python lists.

Happy pythoning.

To cap it all, note that the worst case complexity of the python append to list function is O(1) i.e constant complexity while that of python extend list is O(k) where k is the length of the iterable that is being used to extend the original list.

No comments:

Post a Comment

Your comments here!

Matched content