๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-get-the-second-to-last-element-of-a-list-in-python
How to get the second-to-last element of a list in Python?
March 15, 2026 - def get_second_last(items): if ... 'cherry', 'date'] second_last = fruits[len(fruits) - 2] print(second_last) ... Use list[-2] for the second-to-last element....
Discussions

Second to last element of the list
To find the second-to-last element of a list, we can use indexing techniques. In many programming languages, such as Python, negative indexing allows us to access elements from the end of the list. More on askfilo.com
๐ŸŒ askfilo.com
1
May 20, 2025
How to get the second last element of a list in C++ - Stack Overflow
i just started programing in C++, and have a little bit of experience in C, but in this program was trying to use the C++ libraries that i am not familiar to at all. The objective of the program is More on stackoverflow.com
๐ŸŒ stackoverflow.com
python accessing the second to the last element in a list - Stack Overflow
I have created a dictionary which I have assigned the first element of a list as a key. I would like to append the rest of the elements as values to the dictionary: file_a contains tab delimited ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
HW7.15. Return the second to last element of a list In the function below, return the (single) element from the input list input_list which is in the second to last position in the list. Assume that the list is large enough. student.py 1 - def return_second_to_last_element(input_list):
Answer to HW7.15. Return the second to last element of a list More on chegg.com
๐ŸŒ chegg.com
1
October 13, 2020
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ looping until the second to last element in a list, what's best practice?
r/learnpython on Reddit: Looping until the second to last element in a list, what's best practice?
January 11, 2024 -

edit: forgot to mention I want the indeces not just the items

the methods I can think of are:

for i in range(len(arr) - 1)

for i, e in enumerate(arr[:len(arr) - 1])

I know range(len(arr)) is frowned upon, but I don't see how it's worse than enumerate in this case. In fact using enumerate on a shortened list and calling len to find the second to last element of that list seems extremely clunky and far less readable.

What's the best practice for doing this?

๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ python-get-second-last-element-of-list-exampleexample.html
Python Get Second Last Element of List Example - ItSolutionstuff.com
October 30, 2023 - you will do the following things for python array get second last element. In this example, I will create a simple list with the day with the name. Then I will get second last element with name using -2 key of array.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-get-last-element-in-list
Python: Get Last Element in List
March 2, 2023 - This means that we can access elements ... an index of n-1. The negative indexing follows the same logic, but in reversed order. The last element has the index of -1, the second to last element ......
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ raptor analytics
Find 2nd last or nth last element of a list - YouTube
Find 2nd last or nth last element of a listGIT Link :https://github.com/Amritesh-kumar/Python-Basic/blob/main/Find 2nd last or nth last element%2
Published: October 13, 2023
Views: 247
๐ŸŒ
How to Use Linux
howtouselinux.com โ€บ home โ€บ 3 ways to get last element of a list in python
3 Ways to Get Last Element of a List In Python - howtouselinux
October 9, 2025 - The most effective way to get the last element of a list in Python is using the list[-1]. Python allows you to use negative indices, which count from the end of the list instead of the beginning. So list[-1] gets the last element, list[-2] gets the second to last.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-get-the-last-element-of-a-list-in-Python
How to get the last element of a list in Python?
August 23, 2023 - In Python, getting the last element of a list is a common operation. There are several approaches to accomplish this task, each with different use cases and behaviors ? Python supports negative indexing, where -1 refers to the last element, -2 to the second-to-last, and so on.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-find-the-second-last-element-of-sllist
How to find the second last element of SLList
Line 20: We check the list to see if it is empty or contains one element by examining head and head->next. Lines 25โ€“26: We initialize two node pointers, current with head and previous with nullptr. Lines 28โ€“30: We traverse the list using the while loop until the current pointer reaches the last node. We update the previous pointer to point to the current node previous = current and advance the current pointer to the next node current = current->next. Line 33: We return the value of the second last node, return previous->value.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to get the last element of a python list?
How to Get the Last Element of a Python List? - Be on the Right Side of Change
September 27, 2020 - To access the last element of a Python list, use the indexing notation list[-1] with negative index -1 which points to the last list element. To access the second-, third-, and fourth-last elements, use the indices -2, -3, and -4.
Top answer
1 of 2
2

It's simply a matter of iterating over the list and reporting list[n-1] when list[n]>my_limit.

You will need to extend this to cope with edge cases (i.e. this will error if the first value is over the limit etc.)

And you might want to split your list into one list per commodity as this will trigger when commodities change too.

my_limit = 0.2

for n in range(1, len(my_list)):
    value = float(my_list[n][2].replace(",", "."))
    prev_value = float(my_list[n-1][2].replace(",", "."))

    if value > my_limit and prev_value < my_limit:
        print(my_list[n-1])
2 of 2
1

you can use the itertools module for this

>>> import itertools
>>> my_list = [
    ['Morocco', 'Fish', '0,012'],
    ['Morocco', 'Fish', '0,153'],
    ['Morocco', 'Fish', '0,114'],
    ['Morocco', 'Fish', '0,109'],
    ['Morocco', 'Fish', '0,252'],
    ['Spain', 'Fish', '0,012'],
    ['Spain', 'Fish', '0,113'],
    ['Spain', 'Fish', '0,116'],
    ['Spain', 'Fish', '0,250'],
    ['Spain', 'Fish', '0,266'],
    ['Italy', 'Fish', '0,112'],
    ['Italy', 'Fish', '0,025'],
    ['Italy', 'Fish', '0,224'],
    ['Italy', 'Fish', '0,256'],
    ['Italy', 'Fish', '0,245']]
>>> my_limit = 0.2
>>> for key,sublists in itertools.groupby(my_list,lambda y:y[0]):
        v=[] #we initialize it in case no element fulfill the condition
        for v in itertools.takewhile(lambda x:float(x[-1].replace(",","."))<my_limit ,sublists):
            pass
        if v: 
            print(v,"->",v[-1])

    
['Morocco', 'Fish', '0,109'] -> 0,109
['Spain', 'Fish', '0,116'] -> 0,116
['Italy', 'Fish', '0,025'] -> 0,025
>>> 

here with groupby we, well, group together all the consecutive sublist that have the same value in a given position we specify, which in this case is the first position, then we go into those sublist and take those that fulfill our condition and stop at the first that doesn't with takewhile and from those we only want the last one which will stored into v at the end of the loop.

I make that grouping because that make more sense to my, but if grouping like that isn't necessary, we can also use groupby to split the list into the subsection that fulfill the condition and those that do not by changing the grouping key

>>> my_list = [
    ['Morocco', 'Fish', '0,012'],
    ['Morocco', 'Fish', '0,153'],
    ['Morocco', 'Fish', '0,114'],
    ['Morocco', 'Fish', '0,109'],
    ['Morocco', 'Fish', '0,252'],
    ['Morocco', 'Fish', '0,002'],#extra
    ['Morocco', 'Fish', '0,252'],#extra
    ['Spain', 'Fish', '0,012'],
    ['Spain', 'Fish', '0,113'],
    ['Spain', 'Fish', '0,116'],
    ['Spain', 'Fish', '0,250'],
    ['Spain', 'Fish', '0,266'],
    ['Spain', 'Fish', '0,066'], #extra
    ['Spain', 'Fish', '0,366'], #extra
    ['Italy', 'Fish', '0,112'],
    ['Italy', 'Fish', '0,025'],
    ['Italy', 'Fish', '0,224'],
    ['Italy', 'Fish', '0,256'],
    ['Italy', 'Fish', '0,245'],
    ['Italy', 'Fish', '0,005'],#extra
    ['Italy', 'Fish', '0,305']]#extra
>>> for condition,sublist in itertools.groupby(my_list,lambda x:float(x[-1].replace(",","."))<my_limit):
        if condition:
            for v in sublist:
                pass
            print(v,"->",v[-1])

        
['Morocco', 'Fish', '0,109'] -> 0,109
['Morocco', 'Fish', '0,002'] -> 0,002
['Spain', 'Fish', '0,116'] -> 0,116
['Spain', 'Fish', '0,066'] -> 0,066
['Italy', 'Fish', '0,025'] -> 0,025
['Italy', 'Fish', '0,005'] -> 0,005
>>>    
๐ŸŒ
CodeChef
codechef.com โ€บ learn โ€บ course โ€บ python-development โ€บ PYDEV18 โ€บ problems โ€บ PYTHPROB0759
Accessing the Second-Last Element in Python for project building
Option 1 ยท print("Second-last ... 2 Option 3Option 4 ยท Correct Answer: Option 1 ยท Explanation: Option A is correct because -2 refers to the second-last element, "indigo."...