Try this:

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)
(1, 0)
Answer from Mark Byers on Stack Overflow
🌐
Statistics Globe
statisticsglobe.com › home › python programming language for statistics & data science › get index of item in 2d list in python (2 examples)
Get Column & Row Index of Item in 2D List in Python (2 Examples)
May 4, 2023 - The inner loop also uses the same functions to return the index positions of the columns in the 2D list, ranging from 0 to 2. Remember, Python indexing starts from 0.
🌐
Quora
quora.com › How-do-I-return-the-index-of-an-element-in-a-2D-array-in-Python
How to return the index of an element in a 2D array in Python - Quora
Answer (1 of 2): You don’t return the index - your code needs to keep track of where the elements are. If you are asking how do you search a 2D array - the only way is something like this: [code]def find_all(matrix, element): """Iterate through all row, column indexes in a 2D Matrix where ...
Discussions

Python: Return 2 ints for index in 2D lists given item - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I've been tinkering in python this week and I got stuck on something. If I had a 2D list like this: ... How is myList.index... More on stackoverflow.com
🌐 stackoverflow.com
How to return the index of an element in a 2d array in python? - Stack Overflow
Assuming I want to use the position of an element in a 2d list knowing the element itself, what would be the command to find the index? For a simple list the method list.index(element) works, for e... More on stackoverflow.com
🌐 stackoverflow.com
arrays - Search in 2D list using python to find x,y position - Stack Overflow
I have 2D list and I need to search for the index of an element. As I am begineer to programming I used the following function: def in_list(c): for i in xrange(0,no_classes): if c in c... More on stackoverflow.com
🌐 stackoverflow.com
The fastest way to find indices of element in the 2D python list - Stack Overflow
I have a problem with the efficient implementation of searching indices of elements in 2D list. For example, there are two lists (where list2 have permuted elements from list1): list1 = [[0,1],[2,3... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › help finding the index of a given value within a 2d array
r/learnpython on Reddit: Help finding the index of a given value within a 2D array
November 13, 2022 -

Been messing around with numpy, trying to familiarize myself with it and seeing how I'd be able to utilize its arrays to store (very simple) map data for this text adventure game I've been working on. So far it seems like it'd be pretty darn useful, but I seem to have run into something of an issue.

My code is as follows (note: this is purposefully made with out a main class, because I am just trying to get the basic functionality down in my head before I incorporate it into my main program, and this is just easier for me):

# import numpy module as np
import numpy as np

# Establish the game map, a 3x3 grid of 0's
mainarray = np.array([[0, 0, 0],
                      [0, 0, 0],
                      [0, 0, 0]])

# Give feedback to make console easier to read
print(mainarray)
print("")
print("Placing player in center...")

# Place player on map (represented by a value of 1)
mainarray[1, 1] = 1

print(mainarray)

print("")
print("Locating player...")

# Attempt to find what the current index is of the value 1
print("")
print("Player is at index: ", np.where(mainarray == 1)[0][0])

In my head, I would like to eventually use this np.where() function (if I can) in one of the functions that moves my character. What I want to do is grab the current index of the "player" (represented by the number 1) and attempt to change that value to a 0, and change the value at an adjacent index to 1 (the tile that the player is moving to). Basically, I would like to use each index of the array as a sort of coordinate that I can take and use to move this "1" around the array, setting each index back to "0" after moving the "1", all based off user input.

Anyways, I am not getting any errors, however, the result that gets printed to the console is:

Player is at index:  1

Why is it just one number? It remains the same, even when I remove that second [0] in the last line. I don't fully understand how this function works, as this is the first time I've tried to use it, but since there are no errors, I have no clue what is going on.

Shouldn't I be receiving an index with two values, one for the column and one for the row? If not, how can I grab that as a result and use it in the way I described above? Is it even possible, or am I barking up the wrong tree with this function?

🌐
IQCode
iqcode.com › code › python › python-get-index-of-item-in-2d-list
python get index of item in 2d list Code Example
October 26, 2021 - myList = [[1, 2], [3, 4], [5, 6]] def index_2d(myList, v): for i, x in enumerate(myList): if v in x: return i, x.index(v) print(index_2d(myList, 3)) # you get # (1, 0) ... Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond. Sign up · Develop soft skills on BrainApps Complete the IQ Test ... python find index of element in 2d array python find index in 2d list find index of value in 2d array python find index in 2d array python finding index of 2D list reaching index of elements in 2D list python find index of a row in 2d array python find index of a 2d a
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 70299386 › how-to-get-the-full-index-of-an-object-in-a-2d-list-python
arrays - How to get the full index of an object in a 2D list (Python) - Stack Overflow
How do I get the full index of obj in the 2D array. For example, if obj = o6, the solution should return a[1][2] ... Save this answer. ... Show activity on this post. ... Copya = [[1,2,3], [4,5,6], [7,8,9]] # element that you want to find b = 2 for i,x in enumerate(a): if b in x: index_2d = (i, x.index(b))
🌐
freeCodeCamp
freecodecamp.org › news › python-index-find-index-of-element-in-list
Python Index – How to Find the Index of an Element in a List
May 2, 2022 - If it is Math then we store that index value in a list. We do this entire process using list comprehension, which is just syntactic sugar that allows us to iterate over a list and perform some operation. In our case we are doing decision making based on the value of list item. Then we create a new list. With this process, we now know all the shelf numbers which have math books on them. programming_languages = [["C","C++","Java"],["Python","Rust","R"],\ ["JavaScript","Prolog","Python"]] [ (i, x.index("Python")) for i, x in enumerate(programming_languages) if "Python" in x ]
🌐
Codecademy Forums
discuss.codecademy.com › computer science
Using index location to find item location in 2D list - Computer Science - Codecademy Forums
August 10, 2023 - If I have a 2D list called “shoe_sizes” shoe_sizes = [[“Player0”, “Samir”, 8.5], [“Player1”, “Sally”, 5.6]] and I want to change Player1 shoe size (3rd parameter) how do I call it? Would it be index[1][2] ??
🌐
Stack Overflow
stackoverflow.com › questions › 70023204 › how-to-get-the-second-element-of-index-in-2d-list
python - How to get the second element of index in 2d list - Stack Overflow
The short answer is that you're getting an error because the code if symbol in grid2[0][x] is using x as an index before it is defined.
🌐
Beauty and Joy of Computing
bjc.edc.org › March2019 › bjc-r › cur › programming › old-labs › python › 2D_lists.html
2D Lists in Python
This list of lists allowed for a more comprehensive representation of the board. Creating nested lists can be useful when designing everything from user accounts to game boards. A list within another list is called 2-Dimensional (2D). And just like in a 2D cartesian graph, retrieving an element requires two index values (essentially "the x and y position").
🌐
Carnegie Mellon University
cs.cmu.edu › ~15110-s22 › slides › week4-3-lists.pdf pdf
Lists and Methods 15-110 – Friday 02/11
When you loop over a 2D list and want to access every element, you need to use nested for loops. Often, the outer loop iterates over the indexes of the outer list (rows) and the inner loop iterates over ... This tells Python to call the built-in string function isdigit on the string s.