Get Next Element in List in Python (4 Examples)

 

In this Python article, we will show you how to get the next element after a certain element in a list.

Here is an overview:

Let’s jump right into the examples!

 

Create Sample List

To use in this Python tutorial, we will create a sample list named my_list as follows.

my_list = [1, "apple", 4.2, "banana", 5]
print(my_list)
# [1, 'apple', 4.2, 'banana', 5]

Our sample list named my_list contains two integers (e.g., 1), one float (4.2), and two strings (e.g.,’apple’). How to get the next element, of, for example, ‘apple’? Let’s take a look at some methods!

 

Example 1: Use List Indexing to Find Next Element in List

This method uses the index() method to get the following element after ‘apple’ in my_list. Check it out!

apple_index = my_list.index('apple')
next_element = my_list[apple_index + 1]
print(next_element)
# 4.2

We have used the index() method to find the index of the string ‘apple’ and then added 1 to that index to get the next element after ‘apple’ in my_list!

 

Example 2: Use next() & iter() Functions to Find Next Element in List

Example 2 illustrates how to utilize the next() function with an iterator to get the next element after a certain element in a list. To do this, we can create an iterator from the list using the iter() function, and then call the next() function on the iterator to get the next element.

my_iterator = iter(my_list)
for value in my_iterator:
    if value == "apple":
        next_element = next(my_iterator)
        print(next_element)
# 4.2

As seen, the print() function finally prints the next element after ‘apple’ into the console.

 

Example 3: Use for Loop & enumerate() Function to Find Next Element in List

In this example, we will use a for loop and the enumerate() function to get the next element after the string ‘apple’.

for index, value in enumerate(my_list):
    if value == "apple" and index < len(my_list) - 1:
        next_element = my_list[index + 1]
        print(next_element)
# 4.2

The for loop iterates over my_list, checking each element’s index and value. Then, the if statement checks if the current element’s value is equal to ‘apple’, and if the index of the current element is less than the length of my_list minus 1. The latter condition ensures that we don’t try to get the next element after the last element in the list.

If these previous conditions are met, the next_element is set equal to the element that comes immediately after the current element in my_list. Finally, we print it to the console using the print() function.

 

Example 4: Use List Slicing to Find Next Element in List

This fourth example uses list slicing to get the following element in a list. To do so, we will save the index of the string “apple” in apple_index. Then using slicing, a new list next_element containing only the next element will be defined. Finally, using the 0 index, the element will be extracted.

apple_index = my_list.index("apple")
next_element = my_list[apple_index+1:apple_index+2][0]
print(next_element)
# 4.2

As shown, the next element after ‘apple’ is printed to the console.

Note that not all of these methods might be appropriate for all situations, and you should choose the one that best fits your use case. Make sure you’re getting the next element after the one you want!

 

Video, Further Resources & Summary

Do you need more explanations on how to print the next element after a certain element in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Besides that, you could have a look at other interesting tutorials on Statistics Globe:

This post has shown how to print the next element in a list in Python. Please let me know in the comments section below if you have additional questions.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.

 

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.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top