Python Lists II
In the previous post, we had gone through list creation, indexing and slicing. Let us explore the power of lists further.
Can we append to lists?
The operation of adding elements at the end of a mutable sequence is known as append. Since lists can be modified (aka mutable), it is perfectly legal to apply this operation on objects of type list. To do this, we have the built-in append() function. We can append integers to a list:
Not only single-valued constants like integer, float, boolean and string, a list object can also be appended with another list:
A question that immediately arises, “How do we index a list, like the one shown above, which contains another list?“
We treat the newly added list as an item and index it by position, which happens to be 6 in the above scenario:
“What if we want a particular element of the inner list?”
We go one level further and index by positional value. The first pair of [ ] denotes the entire inner list and next pair of [ ] denotes a particular element within the inner list. Since there are two elements, hello and 449, the legal positional values are 0 and 1 only :
Can we remove elements from a list?
Emptying a list can be accomplished using a single command : clear( )
The operation is performed in-place (clears contents of list object on which the method is invoked), is irreversible and does not return any value. The clear( ) function does not accept any argument:
Situations may arise where we would like to drop one element from a list instead of emptying it. One way of achieving this is to use remove( ) — removes first occurrence of a list item supplied as argument to the function:
Just like clear( ), remove( ) does not return any value, is irreversible and is performed in-place. If we pass an argument which is absent in the list, remove( ) raises a ValueError:
Another way of getting rid of list items is to use pop( ) — removes elements by positional (index) values. It returns the removed element, is irreversible, takes 0 or 1 argument and is applied in-place. We can pass the index value of the item to remove (pass 1 argument):
If we do not pass any argument to pop( ), it returns and removes the last item of the list on which the pop( ) method is invoked:
Lastly, if we pass a positional value for which there is no element in the list, we get an IndexError:
Can we arrange elements of a list in ascending or descending order?
Lists can be sorted using the built-in function sort( ). However, there is an exception. Since list is a heterogeneous sequence (can contain mixed types), it can be sorted only if it contains values of the same type. By default it sorts the elements in ascending order:
The sort( ) function is applied in-place and does not return any value. To arrange elements in descending order, we set the parameter reverse to True (which is False by default; for sorting elements in ascending order):
If we try to sort a list containing values of different types, we get a TypeError:
I hope you found this post helpful. Please feel free to leave your comments, feedback, criticism, thoughts and everything else that comes along with them. See you soon!