I\'m looking to define a function that accepts two parameters: an int
and a list
.
If the function finds the integer in the list it returns
The target will always show up only once and will always be contained in the list
You can use enumerate
to enumerate the outer lists and the elements of the inner lists.
def coords(lst, find):
return next((i, j) for i, sub in enumerate(lst)
for j, x in enumerate(sub)
if x == find)
Demo with your list l
:
>>> coords(l, 2)
>>> (1, 1)
>>> coords(l, 1)
>>> (1, 2)
In case you later want to adapt the function to work properly if the target is not in the list, remember that next takes an optional default
argument.