Introduction
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 the next element added and so on so forth.
Therefore, 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.
The general syntax to create an empty list
s = list()
The general syntax to create a list with some default elements
samp = [e1,e2,e3,....,en]
Where the elements e1, e2, e3, ... can be of different/dissimilar data-types.
We can access the contents of a list object using negative index values as well. The negative index values will start/begin from the 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.
Note – The 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.
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.
<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, we can add only one method. The 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 want to remove. The 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 the case that 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.
Please find the code sample of the list container object as an attachment to this blog, it is a downloadable document containing all the required examples for all the methods discussed above.