Today we’ll be talking about list methods used to sort or reverse lists, determine their length, find the index of an element and so on.
Here’s the video version of the article:
Let’s start with sorting.
Table of Contents
Sorting Lists
We use the sort method to sort a list:
>>> names = ['Bernard', 'Steve', 'Alice', 'Emma', 'Sam', 'Jenny']
>>> names
['Bernard', 'Steve', 'Alice', 'Emma', 'Sam', 'Jenny']
>>> names.sort()
>>> names
['Alice', 'Bernard', 'Emma', 'Jenny', 'Sam', 'Steve']
In case of string elements, the order depends on the Unicode codes, it’s not strictly alphabetical:
>>> words = ["car", "table", "Car", "Zero", "world"]
>>> words.sort()
>>> words
['Car', 'Zero', 'car', 'table', 'world']
I also have an article about sorting lists, so feel free to read it.
Reversing Lists
We use the reverse method to reverse the order of the elements of a list:
>>> names.reverse()
>>> names
['Steve', 'Sam', 'Jenny', 'Emma', 'Bernard', 'Alice']
I also have an article about reversing sequences, so feel free to read it too.
The index Method
We can use the index(x) method to find the index of x. The index of the first x is returned:
>>> champions_1995_to_2000 = ["Italy", "Mexico", "Italy", "Greece", "RSA", "Japan"]
>>> champions_1995_to_2000.index("Italy")
0
Another syntax is:
index(x, startIndex)
Now the search starts at startIndex:
>>> champions_1995_to_2000.index("Italy", 2)
2
Yet another syntax is:
index(x, startIndex, pastIndex)
Now the search starts at startIndex and ends just before pastIndex:
>>> champions_1995_to_2000.index("Italy", 1, 3)
2
If the object is not found, we get an error:
>>> champions_1995_to_2000.index("Italy", 1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'Italy' is not in list
In this article you can read about the index method used with strings.
The len Function
You can use the len function to determine the length of the list. For lists and tuples the length is the number of elements. A sublist counts as one element:
>>> stuff = [1, "abc", "p", [1, 2, 3], -4.21]
>>> len(stuff)
5
The max and min Functions
Just like with strings, we have the max and min functions that return the element with maximum and minimum value respectively:
>>> days = ["Monday", "Friday", "Saturday", "Wednesday", "Tuesday"]
>>> max(days)
'Wednesday'
>>> min(days)
'Friday'
If the elements are strings, they are ordered according to their Unicode codes, so for example uppercase characters precede lowercase ones, hence “Zero” is not maximum:
>>> words = ["car", "table", "Car", "Zero", "world"]
>>> max(words)
'world'
>>> min(words)
'Car'
Here’s how it works with numbers:
>>> numbers = [4.55, 0.002, 1, 6, 2.3]
>>> max(numbers)
6
>>> min(numbers)
0.002
An error is raised if we try the maximum or minimum value in a list containing mixed type data which can’t be compared:
>>> mix = [2, 4, "a", "abc", -3]
>>> max(mix)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
The count Method
We use the method count(x) if we need to know how many times element x occurs in the list:
>>> numbers = [3, 6, 8, 3, 2, 1, 8, 2, 8, 8]
>>> numbers.count(8)
4
>>> numbers.count(11)
0