Hal Lawrence Hal Lawrence - 1 month ago 6
Python Question

How to return the first index occurence of item in lists?

Learning Python and tasked with returning the index location of the first letter in the lists. But it has to be to the left uppermost part on any given list. For example 'a' would return as index (0,2).

When I run my code though, it says the letter isn't found. Assuming Value represents the letters and '.' is already defined in the tester. It should return none if its a '.'

area1 = [['.', 'a', 'a', 'D', 'D'],
['.', '.', 'a', '.', '.'],
['A', 'A', '.', 'z', '.'],
['.', '.', '.', 'z', '.'],
['.', '.', 'C', 'C', 'C']]
def find_spot_values(value,area):
for row in area:# Looks at rows in order
for letter in row:# Looks at letter strings
if value == letter in area: #If strings are equal to one another
area.index(value)# Identifies index?
find_spot_values('D',area1)

Answer

With minimal changes to your code

def find_spot_values(value, area):
    for row in area:  # Looks at rows in order
        for letter in row:  # Looks at letter strings
            if value == letter:  # If strings are equal to one another
                return area.index(row), row.index(letter)  # Identifies indices
Comments