There is: negative indices:
lst[-2]
Answer from Scott Hunter on Stack OverflowSecond to last element of the list
How to get the second last element of a list in C++ - Stack Overflow
python accessing the second to the last element in a list - Stack Overflow
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):
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?
I think you can do:
std::list<int> l = {1, 2, 3}; // (types shouldn't matter), you can do *std::prev(l.end(), 2);
i was trying to get the new second element of the list, using std::next.
You are not getting the new second element of the list, what you are trying to get is the next new address of the list, since what you are passing is the pointer, the address of the list, not the iterator:
list *getNextXValue(list *Head, int x)
{
return std::next(Head, x);
}
Try this instead:
#include <iostream>
#include <list>
#define LOG(x) std::cout << x << std::endl;
typedef std::list<int> list;
std::list<int>::iterator getNextXValue(std::list<int>::iterator Head, int x) {
return std::next(Head, x);
}
/**
* @brief Find last element of a linked list of ints
*
* @return int Program ended correctly
*/
int main() {
list listOne;
list *aux = NULL;
// Inserts all the elements in the list
for (int i = 0; i < 100; i++) {
listOne.insert(listOne.end(), i);
}
listOne.reverse();
auto next = getNextXValue(listOne.begin(), 1);
std::cout << *next << std::endl;
return 0;
}
The Python slice syntax is alist[start:end:step]. So, with your slice ::-1, you are just reversing the list.
If you want the second element to the last, the correct slice would be
alist[1:]
a = {}
for line in file_a.readlines():
split_line = line.strip().split('\t')
a[split_line[0]] = split_line[1:]
a = {}
for line in file_a:
split_line = line.strip().split('\t')
a[split_line[0]] = split_line[1:]
You slicing expression split_line[::-1] evaluates to split_line reversed, because the third parameter is the step (-1 in this case). You want to start at element 1 and go all the way to the end, with the default step of 1. Check this answer for more on slice notation.
There is actually no need to use indices here, as Python loops allow to iterate over elements directly. Then, with simple list slicing you can take the range you want:
integers = [1,3,2,4]
for integer in integers[1:]:
print(integer)
Or, to iterate over elements instead of indexes, but avoid creating a new copy of the list (slices create a new list object), you can use islice:
from itertools import islice
for integer in islice(integers, 1, None):
print(integer)
The range() function
We can generate a sequence of numbers using range() function. range(10) will generate numbers from 0 to 9 (10 numbers). We can also define the start, stop and step size as
range(start,stop,step size). step size defaults to 1 if not provided. This function does not store all the values in memory, it would be inefficient. So it remembers the start, stop, step size and generates the next number on the go.
Your code should looks like:
integer = [1,3,2,4]
for i in range(1,len(integer)):
print (integer[i])
Output:
3
2
4
You're giving input all at once in line number 5 - '2 3 6 6 5'.
Your code expects a single value at a time. Hence for n=5 you need to enter 5 values, one at a time, till your while loop is exhausted.
Solution:
arr=list(map(int, input().split()))
s=len(arr)
sorted(arr)
print(arr[-2])
You were entering a list of elements which were space separated. You don't need n at all. Just split the input and convert each entry to integer and store it in a list.