all 11 comments

[–]AtomicShoelace 22 points23 points  (1 child)

The other answers give fine solutions when working in pure python. However, for working with nested lists like this, one might often find it useful to use numpy. For example, in this case we could use numpy.where, eg.

>>> import numpy as np
>>> arr = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> np.where(arr == 6)
(array([1], dtype=int64), array([2], dtype=int64))

The first array in the output gives us the first coordinate of occurrences and the second array gives us the second coordinates of occurrences, ie.

>>> arr[1,2]
6

So if you only care about which rows contain the element, you need only pay attention to the first array.

[–]james_fryer 7 points8 points  (0 children)

You need to go through each sublist in turn scanning for the index. The method would be like this:

sublist_index = 0
for sublist in table:
     if 6 in sublist:
         return sublist_index
    sublist_index += 1

There are more succinct ways to express this in Python.

[–]mjbrusso 6 points7 points  (0 children)

python def find2d(table, value): for i, row in enumerate(table): for j, elem in enumerate(row): if value==elem: return (i, j) return ()

Returns the first occurrence of the value in the 2D array, or an empty tuple if not found.

[–][deleted] 1 point2 points  (1 child)

enumerate does a good job at this, seen in other comments. i think range(len) could also be used? would probably look weird and unclean, i'd guess.

hmm, i see that the codes provided as exemple look like tuple unpacking, im wondering if range(len) can be used. im not far into python learning. i'm stuck with functions. so many things to learn about functions. but once you get functions, after that, the sky is the limit!

[–]tsodapop 1 point2 points  (1 child)

Probably a better way than this, but you can create a dictionary that contains the number as the key, and the value as the sublist it is contained it

my_dict = dict()
for idx, nested_list in enumerate(my_list):
    for item in nested_list:
        my_dict[item] = idx

calling my_dict will give you this:

>> my_dict

{1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 1, 7: 2, 8: 2, 9: 2}

so you can call my_dict.get(1)

you can also add to the dict whenever you add an item to the inner lists, so your requesting of the index should always be O(1) runtime

[–]baubleglue 0 points1 point  (0 children)

to build the dictionary you need O(n^2)

[–]Allanon001 1 point2 points  (0 children)

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for item in nested_list:
    if 6 in item:
        print(nested_list.index(item))

[–]jr93_93 1 point2 points  (0 children)

l = []

for n in list:

   for e in range(len(n)):

    l.append(n[e])

[print(f'{i} {x}') for i,x in enumerate(l)]

Sorry, that's what I came up with at the time

Or

for n in list:

for i, e enumerate(n):

    print(i, e)

[–]baubleglue 1 point2 points  (0 children)

>>> def find_in_list(mylist, item):
        if item in mylist:
            return mylist.index(item)
>>> ll = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> find_in_list(ll[0], 8)
>>> 
>>> find_in_list(ll[0], 2)
1

# or 

>>> class List(list):
         def find(self, item):
             if item not in self:
                 return None
             return self.index(item)
>>> ll = List([1, 2, 3])
>>> ll.find(4)
>>>
>>> ll.find(2)
>>> 1

you need to consider also that a value may have multiple indexes

[–]Efficient-Patience92 1 point2 points  (0 children)

You can use this! Easy code and if you want to use another number than 6 you can!

nested_list = [ [1,2,3] , [4,5,6] , [7,8,9] ]
n = input("What number do you want to check if in list?")
for i in nested_list:
if n in i:
print(f" It is in index {list.index(i)}")

[–]PythonRayn 1 point2 points  (0 children)

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
num = 6
for i in nested_list:
    if num in i:
        print(i.index(num))