This question already has answers here:
How to find the last occurrence of an item in a Python list
(15 answers)
Closed 8 years ago.
For example, I have a list
[0,2,2,3,2,1]
I want to find the index of the last '2' that appears in this list.
Is there an easy way to do this?
You can try the following approach. First reverse the list, get the index using L.index().
Since you reversed the list, you are getting an index that corresponds to the reversed, so to "convert" it to the respective index in the original list, you will have to substract 1 and the index from the length of the list.
n = ...
print len(L) - L[::-1].index(n) - 1
Related
This question already has answers here:
Pythonic way to find maximum value and its index in a list?
(11 answers)
Getting the index of the returned max or min item using max()/min() on a list
(23 answers)
Closed 1 year ago.
list = [3,5,1,8,9]
I want to find positions of the maximum value in the list
This is pretty simple, but it will give you the index of the first occurrence:
>>> l = [3,5,1,8,9]
>>> l.index(max(l))
4
I strongly suggest you not use the name of built-in functions as list for variables.
This question already has answers here:
Writing a function that alternates plus and minus signs between list indices
(7 answers)
Closed 2 years ago.
Given a list of integers, I want to subtract all the integers with an odd index and add all the integers with an even index. Is there any compressed way to do this without the regular "while loop with iterator and add/subtract"?
Use slicing with a step:
n = sum(L[0::2]) - sum(L[1::2])
The sequence slicing syntax L[i:j:k] is documented here, specifically refer to the note 5.
This question already has answers here:
Find the index of the second occurrence of a string inside a list
(3 answers)
Find the index of the n'th item in a list
(11 answers)
Closed 7 years ago.
If I'm working with a list containing duplicates and I want to know the index of a given occurrence of an element but I don't know how many occurrences of that element are in the list, how do I avoid calling the wrong occurrence?
Thanks
I don't know that a single builtin does this thing alone, but you could fairly easily write it, for instance:
def index_second_occurence(alist, athing):
if alist.count(athing) > 1:
first = alist.index(athing)
second = alist[first + 1::].index(athing)
return second + first + 1
else:
return - 1
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 7 years ago.
I am working of Project Euler problem 2. I need to find the sum of all even Fibonacci numbers up to four million. I have a function that generates all possible Fibonacci numbers up to upperBound which is input by the user. I am passing this list of fibs to another function in order to eliminate the odd values. I am trying to iterate through the list and delete the element if it is odd. It is returning an error saying :
in evenFibs
del list_of_fibs[value]
IndexError: list assignment index out of range
Here is my code for evenFibs() :
def evenFibs(upperBound):
list_of_fibs = getFibs(upperBound)
for value in list_of_fibs:
if value % 2 != 0:
del list_of_fibs[value]
return list_of_fibs
I am not sure why this error is occuring.
Take a note that you shouldn't change array while iterating it, also to delete element from array you should use index of element, not value. You can get index of first element with specific value using index method of list. As for your task, it would be better to use list comprehension:
def evenFibs(upperBound):
list_of_fibs = getFibs(upperBound)
return [value for value in list_of_fibs if value % 2 == 0]
This question already has answers here:
How to get last items of a list in Python?
(5 answers)
Python - How to extract the last x elements from a list [duplicate]
(4 answers)
Closed 9 years ago.
e.g., for a sequence of unknown length, what is the most "Pythonic" way of getting the last n elements?
Obviously I could calculate the starting and ending indices. Is there anything slicker?
Yes, by using negative indices:
last_five = somesequence[-5:]
Negative indices in a slice are relative to the sequence length.
Use negative indexing.
seq[-1] is the last element of a sequence. seq[-3:] gives you the last three.
Try:
sequence[-n:]
(text to make Stack Overflow happy)