CodingBison

Python Lists have several inbuilt methods that provide utilities for doing list-related operations. To see these methods, let us first print all the attributes (some of the attributes are callable methods) supported by lists using the dir() function.

 [user@codingbison]$ python3
 Python 3.2.1 (default, Jul 11 2011, 18:55:33) 
 [GCC 4.6.1 20110627 (Red Hat 4.6.1-1)] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> 
 >>> varList = []
 >>> dir(varList)
 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', 
 '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', 
 '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', 
 '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', 
 '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 
 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
 >>> 

In the above list, attributes without the leading double underscores and trailing double underscores are the callable methods or objects. Thus, the above output shows the following methods for list objects: append(), count(), extend(), index(), insert(), pop(), remove(), reverse(), and sort(). Let us understand these methods.

Searching list elements

We use index(x) method to find the first occurrence of object x in the list. This method takes two additional (optional) arguments as in index(x, i1, i2). If we specify just i1 as in index(x, i1), then this means that index() searches for x starting (and including) the index i1. If we specify both i1 and i2 as in index(x, i1, i2), then this means that index() searches for x starting (and including) the index i1 but less than (and thus, excluding) the index i2.

We can use the count(x) method to find the number of occurrences of the object x in the list.

Here is an example that demonstrates the usage of index() and count() methods.

 >>> varList = [237, 80, 280, 101, 80, 15, 80] 
 >>> print(varList)
 [237, 80, 280, 101, 80, 15, 80]
 >>> 
 >>> varList.index(80)
 1
 >>> varList.index(80, 5)
 6
 >>> varList.index(80, 3, 6)
 4
 >>> varList.count(80)
 3
 >>> 

Adding/deleting list elements

Python provides several methods for adding/deleting list elements: append(), insert(), extend(), pop(), and remove().

Methods append() and insert() add elements to a list. However, these two methods differ in one aspect. Method append() adds elements at the end of the list but method insert() can add elements at any specified index. Thus, insert(i,x) inserts the object x at index i.

In addition to append()/insert(), we can also use extend() method to add elements to a list. However, this method takes a list as an argument and appends the specified list at the end of the existing list.

Here is an example that demonstrates the usage of append(), insert(), and extend() methods:

 >>> varList = list(range(5))
 >>> print(varList)
 [0, 1, 2, 3, 4]
 >>> 
 >>> varList.append(237)
 >>> print(varList)
 [0, 1, 2, 3, 4, 237]
 >>> 
 >>> varList.insert(2, 280)
 >>> print(varList)
 [0, 1, 280, 2, 3, 4, 237]
 >>> 
 >>> varList2 = list(range(11,15))
 >>> print(varList2)
 [11, 12, 13, 14]
 >>> 
 >>> varList.extend(varList2)
 >>> print(varList)
 [0, 1, 280, 2, 3, 4, 237, 11, 12, 13, 14]
 >>> 

Methods append() and insert() have two counterpart methods: pop() and remove(). Without any argument, the pop() method removes the last element from the tail of the list. We can also pass an index to pop() method and with that pop() will remove the element sitting at that index. On the other hand, remove() method can remove a specified element in the list e.g. remove(x) will remove the first occurrence of object x from the list. Thus, if there are multiple occurrences of object x, then remove(x) will remove the first occurrence of x but leave the rest intact.

Besides pop() and remove(), we can also use the "del" operator to delete a single list element or a slice of list elements.

Here is an example that demonstrates pop(), remove(), and del:

 >>> varList = list(range(10))
 >>> print(varList)
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>> 
 >>> varList.pop()
 9
 >>> print(varList)
 [0, 1, 2, 3, 4, 5, 6, 7, 8]
 >>> 
 >>> varList.pop(2)
 2
 >>> print(varList)
 [0, 1, 3, 4, 5, 6, 7, 8]
 >>> 
 >>> varList.remove(3)
 >>> print(varList)
 [0, 1, 4, 5, 6, 7, 8]
 >>> 
 >>> del varList[2]
 >>> print(varList)
 [0, 1, 5, 6, 7, 8]
 >>> 

Arranging list elements

Lists provide methods that help us arrange elements in the list like sort() and reverse(). Method sort() arranges the list elements in an ascending order. Method reverse() reverses the list elements -- thus, the last element becomes the first one, the second element from the last becomes the second element from the start, and so on. Both of these methods arrange elements of the list in-place.

 >>> varList = [237, 80, 280, 101]
 >>> 
 >>> varList.sort()
 >>> print(varList)
 [80, 101, 237, 280]
 >>> 
 >>> varList.reverse()
 >>> print(varList)
 [280, 237, 101, 80]
 >>> 

Note that we can also pass an argument ("reverse") to the sort() method -- this argument accepts a boolean value (True or False), where we can specify if we want the sorted elements to be reversed as well. By default, the "reverse" argument is False. This way, we can do both sort() and reverse() in one step!

 >>> varList = [237, 80, 280, 101]
 >>> 
 >>> varList.sort(reverse=True)
 >>> print(varList)
 [280, 237, 101, 80]
 >>> 




comments powered by Disqus