Python Lists II

Soumyajit Pal
Analytics Vidhya
Published in
4 min readJul 15, 2020

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:

Appending 99 to my_list

Not only single-valued constants like integer, float, boolean and string, a list object can also be appended with another list:

Appending a list to my_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:

Grabbing the entire list using indexing by positional value

“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 :

Using two levels of indexing

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:

Clears my_list completely

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:

Out of two occurrences of 12, the one at index 0 is dropped

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:

Since 99 is absent in my_list, we get 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):

Passing index value of 15 (= 3) to remove it from the list

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:

Removes “hello” (at last index = 6) from my_list

Lastly, if we pass a positional value for which there is no element in the list, we get an IndexError:

my_list ranges from indices 0 to 6, making 10 an invalid argument to pop( )

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:

Sorting strings lexicographic-ally 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):

Sorting my_list_3 in descending order

If we try to sort a list containing values of different types, we get a TypeError:

my_list contains strings and integers and thus cannot be sorted

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!

--

--