# Find Index of List Element Using Recursion in Python (2 Examples)

Hi! This short tutorial will show you how to get the index of a list element using recursion in the Python programming language.

Recursion is a programming technique where a function calls itself to solve a problem by breaking it down into smaller instances.

It involves a base case that determines when the recursion ends and a recursive case that performs computations and calls itself with modified input.

Here is an overview of this tutorial:

Letâ€™s get into the Python code!

## Create Example List

Here, we will create the example Python list of integers that we will apply a recursive function on.

So, in your Python programming IDE, run the code below to create the example list:

```my_list = [2, 4, 2, 6, 2, 8]

print(my_list)

# [2, 4, 2, 6, 2, 8]

print(type(my_list))

# <class 'list'>```

With the example list created, we will now examine ways to use recursion to determine the index of an element in a list.

The target element in the list is defined below:

`target_element = 2`

Letâ€™s see the examples!

## Example 1: Determine Index of First Occurrence of Target Element in List

In this first example, we will get the index of the first occurrence of the target element in the list:

```def find_index_recursive(lst, element, index=0):
if not lst:
return None
if lst[0] == element:
return index
return find_index_recursive(lst[1:], element, index + 1)

first_occurrence_index = find_index_recursive(my_list,target_element)

print(first_occurrence_index)

# 0

print(type(first_occurrence_index))

# <class 'int'>```

Here, the recursive function `find_index_recursive` is used to find the index of the first occurrence of a given element in a list.

The function takes three parameters: `lst`, which is the list being searched, `element`, the target element to find, and `index`, which keeps track of the current index in the list.

The function checks two base cases: if the list is empty (`if not lst`), indicating that the element is not found, it returns `None`. If the first element of the list matches the target element, it returns the current index.

If neither of the base cases is met, the function makes a recursive call to itself with the sublist starting from the second element (`lst[1:]`) and increments the index by `1`.

This process continues until either the element is found or the list becomes empty.

## Example 2: Determine Index of Last Occurrence of Target Element in List

In this next example, we are going to get the index of the last occurrence of the target element in the list:

```def find_index_recursive(lst, element, index=0):
if not lst:
return None
found_index = find_index_recursive(lst[1:], element, index + 1)
if found_index is not None:
return found_index
if lst[0] == element:
return index
return None

last_occurrence_index = find_index_recursive(my_list,target_element)

print(last_occurrence_index)

# 4

print(type(last_occurrence_index))

# <class 'int'>```

In the above example, the recursive function checks two base cases: if the list is empty (`if not lst`), it indicates that the element is not found, and it returns `None`.

If a recursive call to the function on the sublist starting from the second element returns a non-`None` value (indicating that the element is found later in the list), it immediately returns that value.

Otherwise, if the first element of the list matches the target element, it returns the current index.

If neither of the base cases is met, the function makes a recursive call to itself with the sublist starting from the second element (`lst[1:]`) and increments the index by `1`.

This process continues until either the last occurrence of the element is found or the list becomes empty.

## Video, Further Resources & Summary

Do you need more explanations on how to find the index of a list element using recursion in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find the index of a list element using recursion in Python.

The YouTube video will be added soon.

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown, using two examples, how to find the index of a list element using recursion in Python. The context of the problem will determine which solution to use.

I do hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyiâ€™s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Top