Introduction
This article will give you a good understanding of a List. The article also contains the methods which can be used with the List object. Explanations about each method along with the syntax and code samples can be found. After having an understanding of List container object in Python, one can explore more about the list object and also explore other container objects like Tuple, Set or, Dictionary. Hope you will enjoy reading the contents.
A list container object is used to store multiple heterogeneous (elements of dissimilar data-types) elements. A list is a mutable container as part of Python programming language. A list maintains the order of the elements in it. That means that it remembers the first element added and next element added and so on so forth the last element in the list, hence we can access the elements in the list using index values (similar to arrays in any other programming language). The index values start/begin from 0 for the first element and n-1 is the last element index when the list contains n-elements. To access the first element in the list the index is 0 and the second element can be accessed using 1, so and so forth. In general, the i-th element in a list container can be accessed using the index value i-1.
General syntax to create an empty list,
s = list()
General syntax to create a list with some default elements,
samp = [e1,e2,e3,....,en]
Where the elements e1, e2, e3, ... en can be of different/dissimilar data-types.
Code sample
The function len(<listObject>) – will display the total number of elements in the list.
To access the first element in the above list container object.
To access the last element in the above container object.
We can access the contents of a list object using negative index values as well. The negative index values will start/begin from last element as -1 and last but one element index is -2 so on, the first element index is –n where the total number of elements in the list is n.
Therefore we can access the elements in a list using both positive index values starting from 0 to n-1 or using negative index values -1 to –n where n is the total number elements in the list.
To access the last element in the list.
To access the last but one element in the list.
Note
Index value whether positive or negative must be within the range that is from 0 to n-1 or -1 to –n. In case we try to access the elements in a list beyond the allowed range of index values which is completely based on the total number of elements that a list object has we will get “error” from the Python interpreter.
Error when we try to access elements in a list which is beyond the valid range is as follows.
The above error is generated as there are only 5 elements/items in the list object and any value less than -5 or greater than 5 will yield above error.
Accessing the elements in the list from the last element to the first element using positive index values.
The following code snippet is used to display elements using positive index values from the last element to the first element (that is displaying the contents of the list in reverse order).
Working with the methods of a List container object
<listobject>.append(<element>)
The append() method is used to add a new element to the list object, it can add only one element at a time, it will append the element as the last element to the list. Some of the examples are listed below.
Adding more than one element to the list using the append() function/method will yield an error as shown below.
<listObject>.insert(<index>,<element>)
This method is used to add an element at a given index position for a given list object. Using this method as well we can add only one method. Index value must be within the range of the already existing items/elements in the list object, in case the given index value is beyond the range the new element will be either added as the first element or as the last element to the list object.
<listObject1>.extend(<listObject2>)
This function/method can be used to extend the contents of the listObject1 contents by listObject2 elements.
<listObject>.count(<element>)
This function/method is used to count how many times an element occurs as part of the list. As we can store duplicate elements/values/items in a list this function is used to find how many times an element is found in the list. If the given element to the count function is not there in the list then the output is zero.
<listObject>.index(<element>)
The index function is used to find the index of the first occurrence of the given element in the given list of elements. In case the given element doesn’t exist we will get an error.
<listObject>.pop([index=-1])
The pop() function will remove the last element from the given list object as the default index value is -1, in case we want to remove any other item/element from the list object we can give the index value of the element that we can to remove and, index value must be within the range otherwise we will get an error.
<listObject>.remove(<element>)
The remove() function will remove the given element from the list, in case the given element is not in the given list object we will get an error.
<listObject>.sort([reverse=False])
The function sort() will sort the given list object contents in ascending order by default in case we want to sort the elements in descending order then the reverse argument value must be changed to True. All the elements in the list must be of the same type only then the sort function can arrange the elements in the list in default or given order otherwise, we will get an error.
Conclusion
As part of Python programming language, when we want to deal with more than one element/item it is recommended to use a container object list. By using a single variable name, we can store and work with multiple elements/items/values in a list object. Since a list object is mutable we can add and remove elements in the list, index-based access is also possible with the list container object as we have seen in the above code snippets. Sorting/arranging the data is also possible.