List Methods in Python
Video Tutorial
Overview
A list is a data structure in Python that is a mutable or changeable, ordered sequence of elements. Each element or value inside a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ]. Lists being mutable provides the functionality of adding and deleting/removing elements into them.
Scope
In this article, we’ll learn about Lists in Python, their implementation and uses, then we’ll learn about various methods and functions to remove/delete elements from Lists.
Introduction
Every Language has in-built data structures that are used extensively to access data collection. Python includes several array-like data structures in its standard library, that each has slightly different characteristics. One of them is Lists. While there are various list methods in Python, this article/blog explicitly emphasizes how to remove an element from a List in Python.
Why use List?
Handling different data types in a single collection is not a rare scenario. We can have varied data types like string, integer, float, or a list inside a list. This is not a feasible option in C++ or similar languages which don’t support dynamic data in default data types. In many other programming languages, an Array of String can only explicitly consist of strings. Whereas a List in Python will shine among these by storing varied data types as a single collection, and on top of that, it’s ordered.
The below figure gives a simple idea of how we can define a list in Python supporting varied data types in a single collection. It consists of int, float, string, and also a list.
As we discussed the vitality of List data structures and their peculiarity from other similar data structures, we can deep dive into understanding what Lists are and how to remove an element from the List.
What is a List in Python?
A list in Python is an ordered group of items (or elements). It is a very general structure, and list elements don’t have to be of the same type: you can put numbers, letters, strings, and nested lists all on the same List. In a nutshell, the List functionality resembles the Array data structures in many other programming languages. But compared to them, these are more flexible in terms of properties. For example – Support for various data types in a single List.
Indexing and slicing are also supported by List, and they are also mutable, which means they can be modified after their initialization.
A list can also contain another List in it, which is called a nested collection of List. Python Lists have numerous methods to modify their content, and thus they come in handy to handle dynamic situations. All the elements in a list are enclosed within ‘square brackets’ ([]), and every element in it is separated by a ‘comma’ (,).
List Indexing:
- Python lists are like variable-length arrays
- We can access the elements with an index, the starting index is 0. The figure below is a good reference to remember the indexing and slicing
- Indexing is essential to get a grasp of list methods in Python, which can be used to remove elements from lists
- Another indexing technique is slicing. It is used to extract a certain part of a list.
Let consider a list[x:y
] - x is the start position
- y is the exclusive index thus we get values till the y-1 index
- We can also use negative indexing. Like A[-1] points to the last element in the List, A[-2] to the penultimate one, and so on
- The third parameter(z) is the step parameter. It helps us to get non-continuous parts of a list. Basically, to get every n-th occurrence of a list
List methods in Python to delete element/s
Imagine a Simple to-do Application. You write daily tasks and remove some as you complete them. It’s an underlying functionality of many applications to delete redundant or non-useful data. As we discussed, List being a mutable data structure, we can modify it according to our requirement.
When we modify a data structure, deleting an item is an essential operation. In order to remove an element from a list in Python, various list methods can be used, and we will dive into them with good examples.
1. list.remove(x): This method searches for the first instance of the given element and removes it. It throws ValueError if that element is not present. If the List contains more than one matching the specified value, only the first one is deleted.
Check out this article to know more about Python list remove() method.
2. list.pop([i]): Removes the item in the List at the index i and returns it. If ‘i’ is not given (no argument passed to the function), remove the last item in the List and return it. It basically returns the rightmost element if the index is omitted, which implies the opposite of the append() operation.
3. list.clear(): Remove all items from the Python list. The easiest way to actually clear a list in place.
4. del(): Another way to remove an element from a list in Python, given its index instead of its value, is the del statement. The only difference between del and pop is that pop returns deleted value from the List, and del does not return anything. The del statement can also be used to remove slices from a list or clear the entire List. Please check the below examples for more clarification.
Summary
- Indexing in Python helps us to fetch any element at a particular position. Python supports zero-based indexing and negative indexing in case we need to access elements from both ends
- Using the remove(x) method, we can remove a single element from a list, searching for the first instance of the given item(x). Triggers ValueError if element not present
- Using the `pop(i) method we can remove a single list element at the desired index and return it. If we don’t specify an index, pop() method removes and returns the last item in the List
- Using the clear() in python, we can remove all elements from the List without returning anything
- Using the del method, we can remove an element by index, without returning anything
- Time Complexity for the above methods, considering n elements in the List and i is the index mentioned:
-
del O(n - i)
-
pop O(n - i)
-
remove O(n)