I have a nested matrix in Python. I want to find the index of an element that contains a specific number among the elements of the matrix. For example, in the list below I want to show an element from my list that 205 is the third number of that element. I want to do the job without using a special function. Thank you for helping me to do this.
My_List = [[254, 47, 845, 698], [12, 5, 7, 36], [2, 5, 9, 7], [110, 45, 205, 7], [86, 93, 14, 16], [18, 95, 63, 1]]
Asked
Active
Viewed 143 times
-1

louis89
- 5
- 8
-
1what you tried to achieve this? can you please help us with some efforts of what you tried ? – Moinuddin Quadri Dec 27 '20 at 21:22
-
I have no idea how to do this. I'm new to Python. – louis89 Dec 27 '20 at 21:24
-
Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "I have no idea" is off-topic for Stack Overflow; this site is not intended to replace existing tutorials and documentation. Working through those is your job before you post here. – Prune Dec 27 '20 at 21:42
1 Answers
2
Without a special function:
MyList = ...
def find_index(lst, val):
for i,row in enumerate(lst):
if row[2] == val:
return i
index = find_index(MyList, 205)

Moinuddin Quadri
- 46,825
- 13
- 96
- 126

M Z
- 4,571
- 2
- 13
- 27