0

So I found out how to find the indexes of an item in a 2D list in Python by using this code:

def index_2d(myList, v):
    for i, x in enumerate(myList):
        if v in x:
            return (i, x.index(v))

Usage:

index_2d(myList, 3)
#Result (1, 0)

Source

I want the output to be in two variables (like x = 1, y = 0). How can I do this?

user8233898
  • 43
  • 1
  • 8
  • 4
    It _is_ returning two values. You can capture them like this: `x,y = index_2d(...)` – khelwood Jul 03 '17 at 13:21
  • Read [this](https://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python) for more information about that topic. – P. Siehr Jul 03 '17 at 13:35

1 Answers1

0

In Python, you can return more than one value using tuples.

def f():
    return (1,2,3)

(x, y, z) = f()

In a more pythonic way to do it, you can only separate the values with ',' to indicate that it is a tuple.

x, y, z = f()
JomsDev
  • 116
  • 1
  • 9