Python List Index: Find First, Last or All Occurrences
In this tutorial, you’ll learn how to use the Python list index method to find the index (or indices) of an item in a list. The method replicates the behavior of the indexOf() method in many other languages, such as JavaScript. Being able to work with Python lists is an important skill for a Pythonista of any skill level. We’ll cover how to find a single item, multiple items, and items meetings a single condition.
By the end of this tutorial, you’ll have learned:
- How the Python list.index() method works
- How to find a single item’s index in a list
- How to find the indices of all items in a list
- How to find the indices of items matching a condition
- How to use alternative methods like list comprehensions to find the index of an item in a list
Table of Contents
Python List Index Method Explained
The Python list.index() method returns the index of the item specified in the list. The method will return only the first instance of that item. It will raise a ValueError is that item is not present in the list.
Let’s take a look at the syntax of the index() method:
Let’s break these parameters down a little further:
- element= represents the element to be search for in the list
- start= is an optional parameter that indicates which index position to start searching from
- end= is an optional parameter that indicates which index position to search up to
The method returns the index of the given element if it exists. Keep in mind, it will only return the first index. Additionally, if an item doesn’t exist, a ValueError will be raised.
In the next section, you’ll learn how to use the .index() method.
Find the Index Position of an Item in a Python List
Let’s take a look at how the Python list.index() method works. In this example, we’ll search for the index position of an item we know is in the list.
Let’s imagine we have a list of the websites we open up in the morning and we want to know at which points we opened ‘datagy’ .
We can see that the word ‘datagy’ was in the first index position. We can see that the word ‘twitter’ appears more than once in the list. In the next section, you’ll learn how to find every index position of an item.
Finding All Indices of an Item in a Python List
In the section above, you learned that the list.index() method only returns the first index of an item in a list. In many cases, however, you’ll want to know the index positions of all items in a list that match a condition.
Unfortunately, Python doesn’t provide an easy method to do this. However, we can make use of incredibly versatile enumerate() function and a for-loop to do this. The enumerate function iterates of an item and returns both the index position and the value.
Let’s see how we can find all the index positions of an item in a list using a for loop and the enumerate() function:
Let’s break down what we did here:
- We defined a function, find_indices() , that takes two arguments: the list to search and the item to find
- The function instantiates an empty list to store any index position it finds
- The function then loops over the index and item in the result of the enumerate() function
- For each item, the function evaludates if the item is equal to the search term. If it is, the index is appended to the list
- Finally, this list is returned
We can also shorten this list for a more compact version by using a Python list comprehension. Let’s see what this looks like:
One of the perks of both these functions is that when an item doesn’t exist in a list, the function will simply return an empty list, rather than raising an error.
Find the Last Index Position of an Item in a Python List
In this section, you’ll learn how to find the last index position of an item in a list. There are different ways to approach this. Depending on the size of your list, you may want to choose one approach over the other.
For smaller lists, let’s use this simpler approach:
In this approach, the function subtracts the following values:
- len(search_list) returns the length of the list
- 1 , since indices start at 0
- The .index() of the reversed list
There are two main problems with this approach:
- If an item doesn’t exist, an ValueError will be raised
- The function makes a copy of the list. This can be fine for smaller lists, but for larger lists this approach may be computationally expensive.
Let’s take a look at another approach that loops over the list in reverse order. This saves the trouble of duplicating the list:
In the example above we loop over the list in reverse, by starting at the last index. We then evaluate if that item is equal to the search term. If it is we return the index position and the loop ends. Otherwise, we decrement the value by 1 using the augmented assignment operator.
Index of an Element Not Present in a Python List
By default, the Python list.index() method will raise a ValueError if an item is not present in a list. Let’s see what this looks like. We’ll search for the term ‘pinterest’ in our list:
When Python raises this error, the entire program stops. We can work around this by nesting it in a try-except block.
Let’s see how we can handle this error:
Working with List Index Method Parameters
The Python list.index() method also provides two additional parameters, start= and stop= . These parameters, respectively, indicate the positions at which to start and stop searching.
Let’s say that we wanted to start searching at the second index and stop at the sixth, we could write:
By instructing the method to start at index 2 , the method skips over the first instance of the string ‘twitter’ .
Finding All Indices of Items Matching a Condition
In this final section, we’ll explore how to find the index positions of all items that match a condition. Let’s say, for example, that we wanted to find all the index positions of items that contain the letter ‘y’ . We could use emulate the approach above where we find the index position of all items. However, we’ll add in an extra condition to our check:
The main difference in this function to the one shared above is that we evaluate on a more “fuzzy” condition.
Conclusion
In this tutorial, you learned how to use the index list method in Python. You learned how the method works and how to use it to find the index position of a search term. You also learned how to find the index positions of items that exist more than once, as well as finding the last index position of an item.
Finally, you learned how to handle errors when an item doesn’t exist as well as how to find the indices of items that match a condition.
Python Index – How to Find the Index of an Element in a List
Suchandra Datta
When you’re learning to code, you eventually learn about lists and the different operations you can perform on them.
In this article, we’ll go through how you can find the index of a particular element which is stored in a list in Python.
What is a List in Python?
A list in Python is an in-built data type which allows us to store a bunch of different values, like numbers, strings, datetime objects and so on.
Lists are ordered, which means the sequence in which we store the values is important.
List indices start from zero and end at the length of the list minus one. For more detailed information on list data type, check out this comprehensive guide.
Let’s see an example of lists:
Here we created a list of 4 items, where we see that the first item in the list is at index zero, the second item is at index 1, the third item is at index 2, and so on.
For the fruits list, the valid list indices are 0, 1, 2 and 3.
How to Find the Index of Items in a List in Python
Let’s do the reverse. That is, given a list item, let’s find out the index or position of that item in the list.
Python lists provide us with the index method which allows us to get the index of the first occurrence of a list item, as shown above.
We can also see that the index method will raise a VauleError if we try to find the index of an item which does not exist in the list.
For greater detail on the index method, check out the official docs here.
The basic syntax of the index method is this:
We can also specify a sublist in which to search, and the syntax for that is:
To illustrate this further, let’s look at an example.
Suppose we have a book_shelf_genres list where the index signifies the shelf number. We have many shelves containing math books. Shelf numbers also start from zero. We want to know which shelf after shelf 4 has any math books.
We can see the problem here: using just index() will give the first occurrence of the item in the list – but we want to know the index of «Math» after shelf 4.
To do that, we use the index method and specify the sublist to search in. The sublist starts at index 5 until the end of the book_shelf_genres list, as shown in the code snippet below
Note that giving the end index of the sublist is optional. To find index of «Math» after shelf number 1 and before shelf number 5, we will simply do this:
How to Find the Index of a List Item with Multiple Occurrences in Python
What if we need to know the index of a list item which occurs multiple times in a list? The index method won’t give us every occurrence.
In this case, we can find the multiple occurrences using list comprehension as follows:
As shown in this code snippet, we loop over the indices of the list. At each index we check if the item at that index is Math or not. If it is Math then we store that index value in a list.
We do this entire process using list comprehension, which is just syntactic sugar that allows us to iterate over a list and perform some operation. In our case we are doing decision making based on the value of list item. Then we create a new list.
With this process, we now know all the shelf numbers which have math books on them.
How to Find the Index of List Items in a List of Lists in Python
Here we use list comprehension and the index method to find the index of «Python» in each of the sublists.
We pass the programming_languages list to the enumerate method which goes over each item in list and returns a tuple containing the index and item of the list at that index.
Each item in programming_languages list is also a list. The in operator then checks whether «Python» is present in this list or not. If present, we store the sublist index and index of «Python» inside the sublist as a tuple.
The output is a list of tuples. The first item in the tuple specifies the sublist index, and the second number specifies the index within the sublist.
So (1,0) means that the sublist at index 1 of the programming_languages list has the «Python» item at index 0.
How to Find the Index of a List Item that May Not Exist in a List in Python
In many cases, we will end up trying to get the index of an item but we are not sure if the item exists in the list or not.
If we have a piece of code which tries to get index of an item which isn’t present in the list, the index() method will raise a ValueError. In the absence of exception handling, this ValueError will cause abnormal program termination.
Here’s two ways in which we can avoid or handle this situation:
One way is to check using the «in» operator if the item exists in list or not. The in operator has the basic syntax of
where iterable could be a list, tuple, set, string or dictionary. If var exists as an item in the iterable, the in operator returns True. Else it returns False.
This is ideal for our case. We will simply check if an item exists in the list or not and only when it exists we will call index() method. This makes sure that the index() method doesn’t raise a ValueError.
If we don’t want to spend time checking if an item exists in the list or not, especially for large lists, we can handle the ValueError like this:
Wrapping up
Today we learnt how to find the index of an item in a list using the index() method.
We also saw how to use the index method over sublists, how to find the index of items in a list of lists, how to find every occurrence of an item in a list, and how to check for items in lists which may not be present.
I hope you found this article useful and an enjoyable read. Happy coding!
Python: коллекции, часть 2/4: индексирование, срезы, сортировка
В данной статье мы продолжим изучать общие принципы работы со стандартными коллекциями (модуль collections в ней не рассматривается) Python.
Для кого: для изучающих Python и уже имеющих начальное представление о коллекциях и работе с ними, желающих систематизировать и углубить свои знания, сложить их в целостную картину.
ОГЛАВЛЕНИЕ:
- Индексирование
1. Индексирование
1.1 Индексированные коллекции
Рассмотрим индексированные коллекции (их еще называют последовательности — sequences) — список (list), кортеж (tuple), строку (string).
Под индексированностью имеется ввиду, что элементы коллекции располагаются в определённом порядке, каждый элемент имеет свой индекс от 0 (то есть первый по счёту элемент имеет индекс не 1, а 0) до индекса на единицу меньшего длины коллекции (т.е. len(mycollection)-1).
1.2 Получение значения по индексу
Для всех индексированных коллекций можно получить значение элемента по его индексу в квадратных скобках. Причем, можно задавать отрицательный индекс, это значит, что будем находить элемент с конца считая обратном порядке.
При задании отрицательного индекса, последний элемент имеет индекс -1, предпоследний -2 и так далее до первого элемента индекс которого равен значению длины коллекции с отрицательным знаком, то есть (-len(mycollection).
элементы | a | b | c | d | e |
---|---|---|---|---|---|
индексы | 0 (-5) | 1 (-4) | 2 (-3) | 3 (-2) | 4 (-1) |
Наши коллекции могут иметь несколько уровней вложенности, как список списков в примере ниже. Для перехода на уровень глубже ставится вторая пара квадратных скобок и так далее.
1.3 Изменение элемента списка по индексу
Поскольку кортежи и строки у нас неизменяемые коллекции, то по индексу мы можем только брать элементы, но не менять их:
А вот для списка, если взятие элемента по индексу располагается в левой части выражения, а далее идёт оператор присваивания =, то мы задаём новое значение элементу с этим индексом.
UPD: Примечание: Для такого присвоения, элемент уже должен существовать в списке, нельзя таким образом добавить элемент на несуществующий индекс.
2 Срезы
2.1 Синтаксис среза
Очень часто, надо получить не один какой-то элемент, а некоторый их набор ограниченный определенными простыми правилами — например первые 5 или последние три, или каждый второй элемент — в таких задачах, вместо перебора в цикле намного удобнее использовать так называемый срез (slice, slicing).
Следует помнить, что взяв элемент по индексу или срезом (slice) мы не как не меняем исходную коллекцию, мы просто скопировали ее часть для дальнейшего использования (например добавления в другую коллекцию, вывода на печать, каких-то вычислений). Поскольку сама коллекция не меняется — это применимо как к изменяемым (список) так и к неизменяемым (строка, кортеж) последовательностям.
Синтаксис среза похож на таковой для индексации, но в квадратных скобках вместо одного значения указывается 2-3 через двоеточие:
Особенности среза:
- Отрицательные значения старта и стопа означают, что считать надо не с начала, а с конца коллекции.
- Отрицательное значение шага — перебор ведём в обратном порядке справа налево.
- Если не указан старт [:stop:step]— начинаем с самого края коллекции, то есть с первого элемента (включая его), если шаг положительный или с последнего (включая его), если шаг отрицательный (и соответственно перебор идет от конца к началу).
- Если не указан стоп [start:: step] — идем до самого края коллекции, то есть до последнего элемента (включая его), если шаг положительный или до первого элемента (включая его), если шаг отрицательный (и соответственно перебор идет от конца к началу).
- step = 1, то есть последовательный перебор слева направо указывать не обязательно — это значение шага по умолчанию. В таком случае достаточно указать [start:stop]
- Можно сделать даже так [:] — это значит взять коллекцию целиком
- ВАЖНО: При срезе, первый индекс входит в выборку, а второй нет!
То есть от старта включительно, до стопа, где стоп не включается в результат. Математически это можно было бы записать как [start, stop) или пояснить вот таким правилом:
Примеры срезов в виде таблицы:
2.2. Именованные срезы
Чтобы избавится от «магических констант», особенно в случае, когда один и тот же срез надо применять многократно, можно задать константы с именованными срезами с пользованием специальной функции slice()()
Примечание: Nonе соответствует опущенному значению по-умолчанию. То есть [:2] становится slice(None, 2), а [1::2] становится slice(1, None, 2).
2.3 Изменение списка срезом
Важный момент, на котором не всегда заостряется внимание — с помощью среза можно не только получать копию коллекции, но в случае списка можно также менять значения элементов, удалять и добавлять новые.
Проиллюстрируем это на примерах ниже:
-
Даже если хотим добавить один элемент, необходимо передавать итерируемый объект, иначе будет ошибка TypeError: can only assign an iterable
2.4 Выход за границы индекса
Обращение по индексу по сути является частным случаем среза, когда мы обращаемся только к одному элементу, а не диапазону. Но есть очень важное отличие в обработке ситуации с отсутствующим элементом с искомым индексом.
Обращение к несуществующему индексу коллекции вызывает ошибку:
А в случае выхода границ среза за границы коллекции никакой ошибки не происходит:
Примечание: Для тех случаев, когда функционала срезов недостаточно и требуются более сложные выборки, можно воспользоваться синтаксисом выражений-генераторов, рассмотрению которых посвещена 4 статья цикла.
3 Сортировка элементов коллекции
Сортировка элементов коллекции важная и востребованная функция, постоянно встречающаяся в обычных задачах. Тут есть несколько особенностей, на которых не всегда заостряется внимание, но которые очень важны.
3.1 Функция sorted()
Мы может использовать функцию sorted() для вывода списка сортированных элементов любой коллекции для последующее обработки или вывода.
- функция не меняет исходную коллекцию, а возвращает новый список из ее элементов;
- не зависимо от типа исходной коллекции, вернётся список (list) ее элементов;
- поскольку она не меняет исходную коллекцию, ее можно применять к неизменяемым коллекциям;
- Поскольку при сортировке возвращаемых элементов нам не важно, был ли у элемента некий индекс в исходной коллекции, можно применять к неиндексированным коллекциям;
- Имеет дополнительные не обязательные аргументы:
reverse=True — сортировка в обратном порядке
key=funcname (начиная с Python 2.4) — сортировка с помощью специальной функции funcname, она может быть как стандартной функцией Python, так и специально написанной вами для данной задачи функцией и лямбдой.
Пример сортировки списка строк по длине len() каждого элемента:
3.2 Функция reversed()
Функция reversed() применяется для последовательностей и работает по другому:
- возвращает генератор списка, а не сам список;
- если нужно получить не генератор, а готовый список, результат можно обернуть в list() или же вместо reversed() воспользоваться срезом [: :-1];
- она не сортирует элементы, а возвращает их в обратном порядке, то есть читает с конца списка;
- из предыдущего пункта понятно, что если у нас коллекция неиндексированная — мы не можем вывести её элементы в обратном порядке и эта функция к таким коллекциям не применима — получим «TypeError: argument to reversed() must be a sequence»;
- не позволяет использовать дополнительные аргументы — будет ошибка «TypeError: reversed() does not take keyword arguments».
3.3 Методы списка .sort() и .reverse()
У списка (и только у него) есть особые методы .sort() и .reverse() которые делают тоже самое, что соответствующие функции sorted() и reversed(), но при этом:
- Меняют сам исходный список, а не генерируют новый;
- Возвращают None, а не новый список;
- поддерживают те же дополнительные аргументы;
- в них не надо передавать сам список первым параметром, более того, если это сделать — будет ошибка — не верное количество аргументов.
Обратите внимание: Частая ошибка начинающих, которая не является ошибкой для интерпретатора, но приводит не к тому результату, который хотят получить.
3.4 Особенности сортировки словаря
В сортировке словаря есть свои особенности, вызванные тем, что элемент словаря — это пара ключ: значение.
UPD: Так же, не забываем, что говоря о сортировке словаря, мы имеем ввиду сортировку полученных из словаря данных для вывода или сохранения в индексированную коллекцию. Сохранить данные сортированными в самом стандартном словаре не получится, они в нем, как и других неиндексированных коллекциях находятся в произвольном порядке.
- sorted(my_dict) — когда мы передаем в функцию сортировки словарь без вызова его дополнительных методов — идёт перебор только ключей, сортированный список ключей нам и возвращается;
- sorted(my_dict.keys()) — тот же результат, что в предыдущем примере, но прописанный более явно;
- sorted(my_dict.items()) — возвращается сортированный список кортежей (ключ, значение), сортированных по ключу;
- sorted(my_dict.values()) — возвращается сортированный список значений
Отдельные сложности может вызвать сортировка словаря не по ключам, а по значениям, если нам не просто нужен список значений, и именно выводить пары в порядке сортировки по значению.
Для решения этой задачи можно в качестве специальной функции сортировки передавать lambda-функцию lambda x: x[1] которая из получаемых на каждом этапе кортежей (ключ, значение) будет брать для сортировки второй элемент кортежа.
UPD от ShashkovS: 3.5 Дополнительная информация по использованию параметра key при сортировке
Допустим, у нас есть список кортежей названий деталей и их стоимостей.
Нам нужно отсортировать его сначала по названию деталей, а одинаковые детали по убыванию цены.
Перед тем, как сравнивать два элемента списка к ним применялась функция prepare_item, которая меняла знак у стоимости (функция применяется ровно по одному разу к каждому элементу. В результате при одинаковом первом значении сортировка по второму происходила в обратном порядке.
Чтобы не плодить утилитарные функции, вместо использования сторонней функции, того же эффекта можно добиться с использованием лямбда-функции.
-
(на английском). — еще один комментарий с детальным примером ShashkovS к данной статье
UPD от ShashkovS: 3.6 Устойчивость сортировки
Допустим данные нужно отсортировать сначала по столбцу А по возрастанию, затем по столбцу B по убыванию, и наконец по столбцу C снова по возрастанию.
Если данные в столбце B числовые, то при помощи подходящей функции в key можно поменять знак у элементов B, что приведёт к необходимому результату.
А если все данные текстовые? Тут есть такая возможность.
Дело в том, что сортировка sort в Python устойчивая (начиная с Python 2.2), то есть она не меняет порядок «одинаковых» элементов.
Finding the index of an item in a list
Given a list ["foo", "bar", "baz"] and an item in the list "bar" , how do I get its index 1 ?
43 Answers 43
See the documentation for the built-in .index() method of the list:
Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item.
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
Caveats
Linear time-complexity in list length
An index call checks every element of the list in order, until it finds a match. If the list is long, and if there is no guarantee that the value will be near the beginning, this can slow down the code.
This problem can only be completely avoided by using a different data structure. However, if the element is known to be within a certain part of the list, the start and end parameters can be used to narrow the search.
The second call is orders of magnitude faster, because it only has to search through 10 elements, rather than all 1 million.
Only the index of the first match is returned
A call to index searches through the list in order until it finds a match, and stops there. If there could be more than one occurrence of the value, and all indices are needed, index cannot solve the problem:
The list comprehension and generator expression techniques still work if there is only one match, and are more generalizable.
Raises an exception if there is no match
As noted in the documentation above, using .index will raise an exception if the searched-for value is not in the list:
If this is a concern, either explicitly check first using item in my_list , or handle the exception with try / except as appropriate.
The explicit check is simple and readable, but it must iterate the list a second time. See What is the EAFP principle in Python? for more guidance on this choice.
The majority of answers explain how to find a single index, but their methods do not return multiple indexes if the item is in the list multiple times. Use enumerate() :
The index() function only returns the first occurrence, while enumerate() returns all occurrences.
As a list comprehension:
Here’s also another small solution with itertools.count() (which is pretty much the same approach as enumerate):
This is more efficient for larger lists than using enumerate() :
To get all indexes:
index() returns the first index of value!
| index(. )
| L.index(value, [start, [stop]]) -> integer — return first index of value
A problem will arise if the element is not in the list. This function handles the issue:
You have to set a condition to check if the element you’re searching is in the list
If you want all indexes, then you can use NumPy:
It is clear, readable solution.
All of the proposed functions here reproduce inherent language behavior but obscure what’s going on.
Why write a function with exception handling if the language provides the methods to do what you want itself?
Finding the index of an item given a list containing it in Python
For a list ["foo", "bar", "baz"] and an item in the list "bar" , what’s the cleanest way to get its index (1) in Python?
Well, sure, there’s the index method, which returns the index of the first occurrence:
There are a couple of issues with this method:
- if the value isn’t in the list, you’ll get a ValueError
- if more than one of the value is in the list, you only get the index for the first one
No values
If the value could be missing, you need to catch the ValueError .
You can do so with a reusable definition like this:
And use it like this:
And the downside of this is that you will probably have a check for if the returned value is or is not None:
More than one value in the list
If you could have more occurrences, you’ll not get complete information with list.index :
You might enumerate into a list comprehension the indexes:
If you have no occurrences, you can check for that with boolean check of the result, or just do nothing if you loop over the results:
Better data munging with pandas
If you have pandas, you can easily get this information with a Series object:
A comparison check will return a series of booleans:
Pass that series of booleans to the series via subscript notation, and you get just the matching members:
If you want just the indexes, the index attribute returns a series of integers:
And if you want them in a list or tuple, just pass them to the constructor:
Yes, you could use a list comprehension with enumerate too, but that’s just not as elegant, in my opinion — you’re doing tests for equality in Python, instead of letting builtin code written in C handle it:
Is this an XY problem?
The XY problem is asking about your attempted solution rather than your actual problem.
Why do you think you need the index given an element in a list?
If you already know the value, why do you care where it is in a list?
If the value isn’t there, catching the ValueError is rather verbose — and I prefer to avoid that.
I’m usually iterating over the list anyways, so I’ll usually keep a pointer to any interesting information, getting the index with enumerate.
If you’re munging data, you should probably be using pandas — which has far more elegant tools than the pure Python workarounds I’ve shown.
I do not recall needing list.index , myself. However, I have looked through the Python standard library, and I see some excellent uses for it.
There are many, many uses for it in idlelib , for GUI and text parsing.
The keyword module uses it to find comment markers in the module to automatically regenerate the list of keywords in it via metaprogramming.
In Lib/mailbox.py it seems to be using it like an ordered mapping:
In Lib/http/cookiejar.py, seems to be used to get the next month:
In Lib/tarfile.py similar to distutils to get a slice up to an item:
What these usages seem to have in common is that they seem to operate on lists of constrained sizes (important because of O(n) lookup time for list.index ), and they’re mostly used in parsing (and UI in the case of Idle).
While there are use-cases for it, they are fairly uncommon. If you find yourself looking for this answer, ask yourself if what you’re doing is the most direct usage of the tools provided by the language for your use-case.