## How to find the last valid value's index in list or array

I would like to find the last valid value. I found some code for dataframe but can't find for the code for list or array

List = [nan, 1, 4, 6, 8, 122, 12, 34, 54, nan, nan, nan,nan, nan]

The result should look like this:

print(some_function_for_last_valid_index(List)) output : 8

(output is an index)

This is one approach

**Ex:**

import numpy as np lst = [np.nan, 1, 4, 6, 8, 122, 12, 34, 54, np.nan, np.nan, np.nan,np.nan, np.nan] lst = [i for i in lst if not np.isnan(i)] #or lst = filter(lambda x: np.isfinite(x), lst) print(len(lst)) #8

**Find last valid value in a column.,** Ei, guys, what's up? I'm trying to find a formula that returns the last valid number in a column. What i learned so far is a formula that works only if� I would like to find the last valid value. I found some code for dataframe but can't find for the code for list or array List = [nan, 1, 4, 6, 8, 122, 12, 34, 54, nan, nan, nan,nan, nan] The result

You can filter out all nan values and then return the last item. To achieve it you can use build-in function `filter`

new_list = list(fileter(lambda x: x != 'nan', old_list)) print(new_list[-1])

But I don't know why in your example you expect output to be `7`

.

**How to lookup first and last match,** If VLOOKUP finds more than one match, will you get the first match or the last When it reaches the end of the array, it will "step back" to the last valid value� Re: Find last valid value in a column. If the lookup didn't work, the most likely explaination is that your numbers are not really numbers. They are "Numbers Stored As Text"

nan = 'nan' List = [nan, 1, 4, 6, 8, 122, 12, 34, 54, nan, nan, nan,nan, nan] for i in range(len(List)-1,0,-1): if List[i] != nan: print(i) break

outpout:

8

another solution:

while List and List[-1] is nan: List.pop() print(len(List)-1)

outpout:

8

**MS Excel: Get contents of last cell in a range that has data,** =INDEX(range,MATCH(REPT("z",255),range)). where range is the range that you wish to check. For example, if you wanted to find the last text value in column A� To retrieve the last value, we use a combination of “INDEX”, “MAX”, and “COLUMN” functions, and to retrieve the last character in a row, we use the “HLOOKUP” and “REPT” functions in Microsoft Excel 2010. INDEX: The Index formula returns a value from the intersection between the row number and the column number in an Array. There are 2 syntaxes for the “INDEX” function.

**How to return the last value in an Excel data range,** When you need to see the last value in an Excel Table or data range, these formulas will get the If there's no valid value for a record, enter 0. myIndex = Array::LastIndexOf( myArray, myString, 8 ); Console::WriteLine( "The last occurrence of \"{0}\" between the start and index 8 is at index {1}.", myString, myIndex ); // Searches for the last occurrence of the duplicated value in a section of the Array.

**Find last value in a column,** If every match is 1 and the lookup value is 2, the LOOKUP function returns last value in cell range. Normally the list must be sorted ascending for� The valid hosts will be 1 more than the sub and 2 less than the next sub eg. subnet 32+1=33 first valid host, next subnet 64-2=62 last valid host If you can count by 2's, 4's, 8's, 16's, 32's, 64's then good If like me you can only count by 1's don't panic just write out the 16X

pandas.DataFrame.last_valid_index¶ DataFrame.last_valid_index [source] ¶ Return index for last non-NA/null value. Returns scalar type of index. Notes. If all elements are non-NA/null, returns None.

##### Comments

- so should the first
`nan`

be ignored in`List`

? - Possible duplicate of Getting the last non-nan index of a sorted numpy matrix or pandas dataframe
- you mean the last non "nan" value ?
- What have you tried? Please show us your code.
- Oh, my mistake. The output should be 8 not 7
- Still, don't get it... The output should be the last valid item or number of correct items in the list?
- You cant filter out all nan values, in this case he want to keep nan at position List[0]