Is there a way to iterate through a list of lists without getting an index error?

Question:

I have a list of lists and I am trying to pull out every nth term from each list within the list.

Here is my input:

[['', '', '', '', '1', '', '', '', '', '', '', '', '1TD1131D17025-2035', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '09/16/2022', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', 
'', '', '', '', 'EA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '707.20', 'n'], ['', '', '', '', '2', '', 
'', '', '', '', '', '', '1TD1131D17025-2036', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '09/16/2022', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '707.20', 'n'], ['', '', '', '', '3', '', '', '', '', '', '', '', 
'1TD1131D17025-2037', '', '', '', '', '', '', '', '', '', '', '', '', '', '09/16/2022', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '353.60', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '707.20', 
'n']]

Right now I am trying to pull out the first integer from each list.

Here is my sample code.

def find(n,e):
    for line in range(len(line_nu)):
        item = line_nu[n][e]
        n += 1
    return item_nu.append(item)

I’m getting an ‘Index out of range’ error.
I can call ‘ line_nu[0][4] ‘ outside of this loop, but using same numbers in def find() I get an error. I have also tried this as a while loop where I replace n with i and start count at 0. Same error.
End goal is to get each none ” in a list of its own.

Anyone know what I’m doing wrong?

Asked By: McClain

||

Answers:

You’ll need two for loops. For example:

test = [['', '', '', '', '1', '', '', '', '', '', '', '', '1TD1131D17025-2035', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '09/16/2022', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', 
'', '', '', '', 'EA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '707.20', 'n'], ['', '', '', '', '2', '', 
'', '', '', '', '', '', '1TD1131D17025-2036', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '09/16/2022', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '707.20', 'n'], ['', '', '', '', '3', '', '', '', '', '', '', '', 
'1TD1131D17025-2037', '', '', '', '', '', '', '', '', '', '', '', '', '', '09/16/2022', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '353.60', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '707.20', 
'n']

for row in range(len(test)):
    for col in range(len(test[row])):
        print(test[row][col])
Answered By: Fiddling Bits

I interpreted your question as looking for the nth non-empty element in each sub list, so elements != ”. This function will execute that and return an array of the nth non-empty element in each sub array

exl = [['', '', '', '', '1', '', '', '', '', '', '', '', '1TD1131D17025-2035', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '09/16/2022', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', 
'', '', '', '', 'EA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '707.20', 'n'], ['', '', '', '', '2', '', 
'', '', '', '', '', '', '1TD1131D17025-2036', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '09/16/2022', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '707.20', 'n'], ['', '', '', '', '3', '', '', '', '', '', '', '', 
'1TD1131D17025-2037', '', '', '', '', '', '', '', '', '', '', '', '', '', '09/16/2022', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '353.60', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '707.20', 
'n']]

def find(n,listOfLists):
    result = []
    for list in listOfLists:
        discoveredItems = 0
        for item in list:
            if item != '':
                discoveredItems += 1
                if discoveredItems == n:
                    result.append(item)
                    break
    return result

So to get the first non-empty item in each list (the integers you mentioned above)…

find(1,exl)
# ['1', '2', '3']

And the dates (aka 3rd non-empty element):

find(3,exl)
# ['09/16/2022', '09/16/2022', '09/16/2022']
Answered By: jros

assuming your list of lists has the name data, we can get the first integer of each sublist like:

data = [[...],[...],...]
for list in data:
    for item in list:
        if item.isdigit():
            print(item)
            break
Answered By: lroth

If the numbers are always at the same index for all sublists, you can get a list of the numbers (as strings) by looping once:

def find(lists, n):
    out = []
    for lst in lists:
        out.append(lst[n])
    return out

# call it with n=4
numbers = find(mylists, 4)

Obviously, a list comprehension can more appropriate here

def find(lists, n):
    return [lst[n] for lst in lists]
L = [['', '', '', '', '1', '', '', '', '', '', '', '', '1TD1131D17025-2035', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '09/16/2022', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', 
'', '', '', '', 'EA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '707.20', 'n'], ['', '', '', '', '2', '', 
'', '', '', '', '', '', '1TD1131D17025-2036', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '09/16/2022', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '707.20', 'n'], ['', '', '', '', '3', '', '', '', '', '', '', '', 
'1TD1131D17025-2037', '', '', '', '', '', '', '', '', '', '', '', '', '', '09/16/2022', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '353.60', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '707.20', 
'n']]
for _ in range(len(L)):
    print(set(L[_]))

OUTPUT:

{'', '707.20', '353.60', '1TD1131D17025-2035', '2', 'EA', '09/16/2022', '1', 'n'}
{'', '707.20', '353.60', '2', 'EA', '09/16/2022', '1TD1131D17025-2036', 'n'}
{'', '707.20', '353.60', '2', 'EA', '09/16/2022', '1TD1131D17025-2037', 'n', '3'}
Answered By: Bhaskar Gupta
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.