Skip to content
Home » List Methods in Python

List Methods in Python

Spread the love

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.

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.

 

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

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'

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

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 

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply