How to update a list in Python 2.7

Python lists help the coder to store information for later usage in their software development projects. As you may know, one can easily declare a list object in Python computer programming language by using the syntax which is being shown below.

list_object = []

Based on my personal experience with Python computer programming language, a list can have different type of elements in it. To make things more practical, I am writing the following list.

list_object = [1, 2, 3, ‘coder’]

Being mutable objects, Python lists can change in time. What does it mean? It means that once you have declared a list, you can easily update its elements by using the builtin operations supported by the list object.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

The following syntax can be used in Python code to append a new element in a list.

[].append(element)

If one tries to run the above code, their Python interactive shell will throw an error, because the element has not been declared. The example shown above stands only for explaining the append operation, which is specific to list objects in Python computer programming language.

Now, let’s get our hands dirty. Having declared a real list in our Python interactive shell, we can easily update it, by using the append operation; like shown below.

list_object.append(4)

Once the list is updated successfully, we can easily check its new element like shown below.

list_object

The following will get printed on your Python console.

[1, 2, 3, ‘coder’, 4]

Not only do list objects support population with new elements, but they also support depopulation too. The syntax for removing an element from a list is being shown below.

[].pop(index_of_element_to_be_removed)

Let’s try to remove from the list, the element we just added by using the pop operation like shown in the following example.

list_object.pop(4)

If the above piece of Python code is being executed successfully in your Python interpreter shell, you will get the following output on the console.

4

In our example, the index of the element we removed, is the same as the value of the element.

To remove another element from the list_object, use the pop method of the Python list, by providing as an argument the index of the element you want to remove.

Let’s try to remove the element ‘coder’ from the list_object.

list_object.pop(3)

If the above piece of Python code is being executed successfully in your Python interactive shell, you will get the following output.

‘coder’

And now, if we check the list named list_object, we will realize that it has been updated.

list_object

Once the above code is being run, the following comes in the Python console.

[1, 2, 3]

Final thoughts

There is another way which I do know that can be used to update list objects in Python, but I am going to share that in another article, in which I am going to explain Python lists in details for complete beginners. Specific Python list methods such as append and pop, are enough to add new elements to the list, and remove existing elements from the list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *