-1

Supposing you had an array (in the latest version of Python):

list = [
        ['1', '2', '3'], ['4', '5', '6'], ['7', '8', ' ']
    ]

How do you find the index of the value that has the empty (' ') space?

Like list.index(' ') would return [2][2] or smthn.

I think for a 1D list it was string.index() but I was wondering what it was for a 2d List

1 Answers1

1

Use a list comprehension with enumerate:

lst = [
        ['1', '2', '3'], ['4', '5', '6'], ['7', '8', ' ']
    ]

out = [(i,j) for i,l in enumerate(lst) for j,x in enumerate(l) if x == ' ']

output:

[(2, 2)]

If you want only the first found value, else a default value (here 'not found'):

out = next(((i,j) for i,l in enumerate(lst)
             for j,x in enumerate(l) if x == ' '),
           'not found')
mozway
  • 194,879
  • 13
  • 39
  • 75