Python list is a data type used to store multiple items in a single variable. After storing multiple variables in a variable, you may need to find the index of an item in the list to access it.

**In Python, you can find the **index of an item in the **list using the index() method.**

In this tutorial, you’ll learn the different ways to find the index of an item in the list.

**If You’re in Hurry…**

You can use the below code snippet to find the index of an element.

```
list = [1,5,7,9,23,56]
list.index(5)
```

Where, `5`

is existing in the second position in the list. **Since the index is 0 based,** you’ll see the output as

`1`

.**Output**

` 1`

**If You Want to Understand Details, Read on…**

In this detailed tutorial, you’ll learn the different methods available to **find the index of an element in the list.**

Wherever possible, you’ll be using the packages `Numpy`

and `pandas`

to make the find operation easy. This is to avoid the complex core python code implementation.

Table of Contents

## Python List Index() With Examples

You can find the index of an item in the list using List.index() method.

**Syntax**

`list.index(element, start_pos, end_pos)`

Where,

`element`

–**Required**. Element to Find in the list`start_pos`

–**Optional**. If you want to find an item starting from a specific position rather checking entire list`end_pos`

–**Optional**. If you want to find an item until a specific position .

You can use the start position and end position if you want to check if an item is available in the list within the specified range.

**Exception**

If the item is not available in the list, then **value_error** exception will be thrown.

**Examples**

Use the below snippet to find the index of an element `5`

in the entire list.

**Snippet**

```
list = [1,5,7,9,23,56]
list.index(5)
```

Where, `5`

is existing in the second position in the list. **Since the index is 0 based,** you’ll see the output as

`1`

.**Output**

`1`

Now, you’ll use the *start position* and *end position* to find the index of an element in the specified range.

Use the below snippet which includes the *Start position* and *End position* along with the element to find.

**Snippet**

```
list = [1,5,7,9,23,56]
list.index(23, 3,5)
```

Where,

`23`

– Element to find.`3`

– Starting position in the list to find the element`5`

– End position in the list to find the element.

Element `23`

is available in the list and its index is `4`

. Hence you’ll see the below output.

**Output**

`4`

This is how you can find the index of an element in the list.

Next, you’ll learn the different methods available to find the index of an item.

## Find Index Of Item Without Exception

In this section, you’ll learn how to **find the index of an item without exception.**

This can be used when you want to find the index of Item If Exists, otherwise return a default value.

You can do this **by catching the ValueError Exception** and

**returning a default value**in case of this error.

For example, you can return `-1`

if the value is not found.

**Snippet**

```
list = [1,5,7,9,23,56]
try:
index_value = list.index(20)
except ValueError:
index_value = -1
print(index_value)
```

In the example list, value `20`

is **not available.**

Hence a `ValueError`

will be thrown. Then the **exception will be caught** and the value `-1`

will be set to the `index_value`

variable.

So you’ll **not see** the exception in your program.

**Output**

` -1`

This is how you can find the index of an item in the list without exception.

Next, you’ll find an index of multiple elements.

## Find Index Of Multiple Elements

In this section, you’ll learn how to **find the index of multiple elements at once.**

You’ll use the where() method in `NumPy`

package to find the index and isin() method to pass multiple items.

**Snippet**

```
import numpy as np
list = [1,5,7,9,23,56]
idx=np.where(np.isin(list, [5,7]))
idx
```

Where,

`np.isin(list, [5,7])`

– To find index of 5 and 7 in`list`

object. It’ll return an array with`true`

in the place of the items being searched and false in other indices.`np.where()`

– will use the the`booleans`

in the array to return the indexes.

In the list, `5`

and `7`

exists in the second and third positions respectively. Hence you’ll see an array as `[1,2]`

as shown below.

**Output**

`(array([1, 2], dtype=int32),)`

This is how you can find the index of multiple items.

Next, you’ll learn how to find the index of an item in a NumPy array.

## Find Index of Item Using Numpy

In this section, you’ll **find the index of an element in the Numpy array.**

First, you’ll create an array using `np.array()`

.

Then you’ll use the `where()`

method available in the `Numpy`

to find the index of an item.

**Snippet**

```
import numpy as np
list = np.array([1,5,7,9,23,56])
item_index = np.where(list == 5)
item_index
```

Where,

`list = np.array([1,5,7,9,23,56])`

– Creating a`Numpy`

Array`np.where(list == 5)`

– List is the source array name and`5`

is the item to find the index.`item_index`

-To print the index of the item

`5`

is existing in the second position in the `numpy`

array.

Since the **index is 0 based**, you’ll see the output as `array([1])`

as shown below.

**Output**

`(array([1], dtype=int32),)`

This is how you can find the index of an element in the `numpy`

array.

Next, you’ll find the index of items in the list with a specific conditions.

## Find Index Of Item in List with specific condition

In this section, you’ll **find the **index of items in the list with the specific conditions using list comprehension.

**For example,** if you need to find the index of items that is greater than 10, you can use the list comprehension.

The list comprehension will **return a list of index of items that passes the condition**.

Use the below snippet to find the index of items **that is greater than 10**.

**Snippet**

```
list = [1,5,7,9,23,56]
output = [idx for idx, value in enumerate(list) if value > 10]
output
```

Where,

`idx`

– To return the index during each iteration if the condition is true.`for idx, value in enumerate(list)`

– To enumerate the list as index and value`if value > 10`

– To check the value condition during each iteration

The values 23 and 56 are greater than 10 and it is available in the index 4 and 5.

Hence, you’ll see the below output.

**Output**

`[4, 5]`

This is how you can **find the **index of items using list comprehension based on the specific conditions**.**

Next, Find the index of an item in a case insensitive way.

## Find Index Of Item In List Case Insensitive

In this section, you’ll learn how to **find the **index of an item in a list in a case-insensitive** way.**

You need to convert the complete list to either *lower case* or *upper case*.

In the example, you’ll **convert the items in the list to lower case** using the `lower()`

method and *create a new list* with the lower case items.

Then you can invoke the `index()`

method in the new list which has lower case items.

**Snippet**

```
to_find = 'b'
list = ['A', 'B', 'C', 'D', 'E', 'F', 'g', 'h']
lower_list = [item.lower() for item in list]
lower_list.index(to_find.lower())
```

The lower_list will contain the **items in the lower case.**

*B* is available in the second position as `b`

. Since the **index is 0 based**, you’ll see the output `1`

.

**Output**

`1`

This is how you can find an index in the list using case insensitive way.

Next, you’ll learn about regex.

## Find Index Of Item In List Using Regex

In this section, you’ll learn how to **find Index Of Match items** in the **list** using regex.

Regex module provides regular expression matching operations to match a string based on the condition.

First, you need to import the regex module using the below statement.

`import re`

Then you can search for an item using the `re.search(expression, item)`

where,

`Expression`

– Condition to pattern match`item`

– Item which needs to be checked if its matching the expression.

To learn more about the regular expression, refer to this regex cheat sheet.

Use the below snippet to find the index of items that ends** with a.**

**Snippet**

```
import re
countries = ['India', 'United States', 'Canada', 'Germany']
country_idx = [i for i, item in enumerate(countries) if re.search('a$', item)]
country_idx
```

In the sample list, there are two items *India *and *Canada *which ends with `a`

available in position `1`

and `3`

.

Since the **Index is 0 based**, you’ll see the below output.

**Output**

`[0, 2]`

This is how you can find the index of an element using regular expression.

Next, you’ll see about the duplicate elements.

## Find Index Of Duplicate Elements

In this section, you’ll **find the index of duplicate elements in the list.**

You’ll use the packages `numpy`

and `pandas`

for this purpose.

First, you’ll create a dataframe with the list of values and** identify the duplicate values** using the duplicated() method in the dataframe.

Then, you’ll use the `np.where()`

to** identify the index of the duplicate elements.**

**Snippet**

```
import numpy as np
import pandas as pd
list = ['a', 'a', 'c', 'f', 'e', 'f', 'g', 'h']
idx = np.where(pd.DataFrame(list).duplicated())
idx
```

In the sample list, duplicate elements `a`

and `f`

exists in position `2`

and `6`

. Since the index is `0`

based, you’ll see the below output.

**Output**

`(array([1, 5], dtype=int32),)`

This is how you can find the index of duplicate elements.

Next, you’ll see about unique elements.

## Find Index Of Unique Elements

In this section, you’ll **find the index of unique elements in the list.**

You’ll use the unique method available in the `NumPy`

package to find the unique elements.

**Snippet**

```
import numpy as np
list = ['a', 'a', 'c', 'f', 'e', 'f', 'g', 'h']
np.unique(list, return_index=True)
```

Where,

`list`

– List of elements for which unique index to be identified.`return_index=True`

– To return the index of the unique elements along with the unique elements list.

You’ll see the below output which has the **unique elements in the list** and also the **index of the unique elements** in the list as array.

**Output**

```
(array(['a', 'c', 'e', 'f', 'g', 'h'], dtype='<U1'),
array([0, 2, 4, 3, 6, 7], dtype=int32))
```

This is how you can find the index of the unique items in the list.

Next, you’ll learn about the sorted list.

## Find Index Of Item In Sorted List

In this section, you’ll learn **how to find the **index of items in a **sorted list.**

This may be useful when you want to **identify the index after sorting the values in the list.**

First, you’ll **sort the list** using the `sorted()`

method. Then you can invoke the `index()`

method **along with the item to find the index** in the sorted list.

Use the below snippet to identify the index of 1 in the list after sorting it.

**Snippet**

```
list = [25, 55, 86 ,1,23,56]
sorted_list = sorted(list)
print("Sorted List is", sorted_list)
print("\nIndex of 1 in Sorted list is" ,sorted_list.index(1))
```

The list is sorted and the item `1`

is **moved to the first position** of the list. Hence you’ll see the below output.

**Output**

```
Sorted List is [1, 23, 25, 55, 56, 86]
Index of 1 in Sorted list is 0
```

This is how you can find the index of an element in a sorted list.

Next, you’ll learn about finding the largest item.

## Find Index Of Largest Item In List

In this section, you’ll learn how to **find the **index of the **largest item in the list.**

To find the index of the largest element, first, y**ou need to know which is the largest element** in the list.

You can identify the largest value in the list using the `max()`

function.

Then you can find the index of that value using the `index()`

method.

**Snippet**

```
list = [1, 2, 3, 100, 200]
largest_value = max(list)
max_index = list.index(largest_value)
print(max_index)
```

In the sample list, the largest element is `200`

and it **exists in the 5th position** of the list. Since the index is `0`

based, it’ll return the output `4`

.

**Output**

`4`

This is how you can find the index of the largest element in the list.

Next, you’ll learn about the smallest element.

## Find Index Of Smallest Item In List

In this section, you’ll learn how to **find the index of the smallest item in the list.**

To find the index of the smallest element, first, **you need to know which is the smallest element** in the list.

You can identify the smallest value in the list using the `min()`

function.

Then you can find the index of that value using the `index()`

method.

**Snippet**

```
list = [50, 1, 2, 3, 100, 200]
smallest_value = min(list)
min_index = list.index(smallest_value)
print(min_index)
```

In the sample list, the smallest element is `1`

and exists in the 2nd position of the list. Since the index is `0`

based, it’ll return the output `1`

.

**Output**

`1`

This is how you can find the index of the smallest item in the list.

## Find Index Of All Occurrences Of an Item In List

In this section, you’ll learn how to **find the index of all occurrences of an item in the list.**

For example, if a list **contains an item more than once**, then **you’ll find all the indexes** of that item.

There are two ways to find the index of all occurrences of an item.

- Using Numpy.Where()
- Using List Comprehension

To find the index of all occurrences of an item in the array, create an array with an item more than once.

E.g. `[5, 8, 3, 20, 3, 87, 3, 95, 63, 3]`

where 3 occurs more than once.

### Using Numpy

Using NumPy `where()`

, you can pass the list and the item to which the index needs to be identified.

**Snippet**

```
import numpy as np
list = np.array([5, 8, 3, 20, 3, 87, 3, 95, 63, 3])
np.where(list == 3)
```

Where,

`np.array()`

– To create a numpy array with items more than once`np.where(list == 3)`

– To find the positions of 3 in the list.

In the sample list, ** 3 exists in the position 3, 5, 7 and 10**. Since the index is

`0`

based, you’ll see the below output.**Output**

`array([2, 4, 6, 9], dtype=int32)`

This is how you can find all occurrences using the NumPy `where()`

.

Next, you’ll use the list comprehension.

### Using List Comprehension

In this section, you’ll learn how to find all occurrences of an item in the list using list comprehension.

You’ll iterate over the list index and value using the for loop and if the value is 3, then **add that item’s Index to the list comprehension return value.**

You’ll have a new list that contains the index of item 3.

```
list = [5, 8, 3, 20, 3, 87, 3, 95, 63, 3]
idx = [idx for idx, x in enumerate(list) if x == 3]
idx
```

Where,

`idx`

– List comprehension return value`for idx, x in enumerate(list)`

– Enumerate the list with index and value of the list`if x == 3`

– Condition to check during the each iteration.

In the sample list, ** 3 exists in the position 3, 5, 7 and 10**. Since the index is

`0`

based, you’ll see the below output.**Output**

`[2, 4, 6, 9]`

This is how you can identify the index of all occurrences of an item using the list comprehension.

## Conclusion

To summarize, you’ve learned how to find the index of an item in the **list.**

You’ve used the index() method to find the index and also used the packages `numpy`

and `pandas`

where it can be **helpful to find the index of an item in various scenarios without any complex code.**

You’ve also learned about using the regular expressions and list comprehensions to find the index of an element in the list and find the index of all occurrences of an element in the list.

If you have any questions, comment below.