0

If I have a list like

 [[a, b], [c, d], [e, f]] 

How would I know that element a is in index 0 of the big list. I'm unsure on how to do this with a 2 dimensional array.

I tried to use index, but it not works

s = [['a', 'b'], ['c', 'd'], ['e', 'f']] s.index('a') 
Traceback (most recent call last): File "C:/Users/xxy/PycharmProjects/tb/test.py", line 3, in <module> s.index('a') ValueError: 'a' is not in list 

2 Answers 2

1

You can solve it with a loop

s = [['a', 'b'], ['c', 'd'], ['e', 'f']] x = 'a' find = False for i, line in enumerate(s): if x in line: print(f'find {x} at', i, line.index(x)) find = True break if not find: print(f'{x} is not in list') 
Sign up to request clarification or add additional context in comments.

Comments

0

More simpler way you can use any()

s = [['a', 'b'], ['c', 'd'], ['e', 'f']] for i, v in enumerate(s): if any('a' in x for x in v): print(i) 

Gives #

0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.