Discussions

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
python - Find the second last number from a list - Stack Overflow
If you want to cope without sorting, ... outside of the list scope. -1 will suffice with your example, but a better way to do it is setting the starting value so that there is nothing smaller: float('-inf'). ... Your only mistake is to set both largest and second_largest to nums[0], ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How do I get the last element of a list? - Stack Overflow
How do I get the last element of a list in Python? I tried: ... Save this answer. ... Show activity on this post. ... In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to get the second to last value in a list? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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 - 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. so let's see the below example. You can use these examples with python3 (Python 3) version.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-get-last-element-in-list
Python: Get Last Element in List
March 2, 2023 - lastElement = exampleList[-1] print("Last element: ", lastElement) print("exampleList: ", exampleList) secondToLast = exampleList[-2] print("Second to last element: ", secondToLast) ... Negative indexing does not change the original list. It is only a way of accessing elements without any changes ...
๐ŸŒ
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
Find elsewhere
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python get the last element of a list
Python Get the Last Element of a List - Spark By {Examples}
May 31, 2024 - Have you ever needed to get the last element of a list in Python, but weren't sure of the best way to do it? There are several different methods you can
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-how-to-get-the-last-element-of-list
Get the Last Element of List in Python - GeeksforGeeks
July 11, 2025 - Explanation: Last element of the list l in the above example is 6. Let's explore various methods of doing it in Python: The simplest and most efficient method uses negative indexing with a[-1]. In Python, we can use -1 as an index to access ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 73956235 โ€บ find-the-second-last-number-from-a-list
python - Find the second last number from a list - Stack Overflow
You can use np.sort() to sort the array and access the second last element. ... my_nums_sorted = np.sort(np.array(my_nums)) # Convert to np.array and sort second_largest = my_nums_sorted[-2] ... Find the answer to your question by asking.
๐ŸŒ
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 - Use list[-1] for getting the last element as it's the most pythonic and efficient approach.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-get-first-and-last-elements-of-a-list
Get first and last elements of a list in Python - GeeksforGeeks
Unpacking allows us to extract the first and last elements while ignoring the rest. Using first, *_, last = a gives the first and last elements directly.
Published: July 11, 2025
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-slicing-list-from-kth-element-to-last-element
Python - Slicing List from Kth Element to Last Element - GeeksforGeeks
July 11, 2025 - A for loop can be used to extract elements from the Kth index to end by iterating through the list starting at index K.
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
>>>    
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ python-accessing-the-last-element-of-a-list
Python: Accessing the Last Element of a List
August 24, 2023 - In the above example, my_list[-2:] gets the last two elements of the list. You can replace 2 with any number to get that many elements from the end of the list. The itertools module in Python comes with a function called islice() that can be used to get the last n elements of a list.