4

So lets say I have this array :

array = [[1,2,3],[4,5,6],[7,8,9]]

and I want to get the index of the inner array containing the 5 for example. So in this case the returned index I want is 1.

I did try ind = array.index(5) but I'm quite aware why this didnt work since the value in the brackets have to match the element in the array exactly. Another way I did this is

counter = 0
for each in array:
  if 5 in each: break
  else: counter = counter + 1

and this worked well for what I want but I wanted to check if there is an easier and cleaner way to do this. Thanks

2
  • 2
    That seems the most efficient/readable way to me. Commented Apr 20, 2017 at 8:14
  • Readability is very subjective here: a lot of programmers would argue that itertools are more clear than mishmash of cycles with episodic breaks in them. Efficiency: rather no. Itertools Commented Apr 20, 2017 at 8:50

9 Answers 9

7

There is a slightly better pythonic approach using next(..). Remember that if 5 doesn't exist in any of the sub-arrays, this will throw a StopIteration. You might want to handle that.

>>> your_list = [[1,2,3],[4,5,6],[7,8,9]]
>>> next(i for i, x in enumerate(your_list) if 5 in x)
1
Sign up to request clarification or add additional context in comments.

2 Comments

how would this return the index though?
@L.D Yes, that was my mistake. I just fixed it
2

Better avoid breaks. Here is a better way:

for ind, inner_arr in enumerate(array):
    if 5 in inner_arr:
        return ind

Comments

2

one liner solution

#a = [[1,2,3],[4,5,6],[7,8,9]]
[i for i, j in enumerate(a)  if 5 in j][0]

2 Comments

Using next(..) on a generator expression might give you an equivalent of a short-circuit as in my answer without having to construct an entire list
yeah, i've totally forgotten it -))
1

If you still want to use index method, here is a possible approach:

[x.index(5) for x in array if 5 in x]

1 Comment

do not forget to get zeroth item -))
1

What you have seems the way to go, but there are some improvements possible:

def find(item, array):
    counter = 0
    for each in array:
        if item in each:
            return counter
        counter = counter + 1
    return None

But even the counter can be automated:

Update: actually don't need return None - this is the default of all functions

def find(item, array):
    for idx, each in enumerate(array):
        if item in each:
            return idx

Comments

1

As an alternative to the use of enumerate(), I'd go with the following code:

for elem in array:
     for i in elem:
             if 5 in elem: print array.index(elem)
             break

Comments

0
array = [[1,2,3],[4,5,6],[7,8,9]]
counter = 0

for item in array:
    for n in item:
        if n == 5:
            counter += 1
    print counter

Probably more efficient ways to do it, but this works for me

1 Comment

I think you placed counter += 1 in the wrong for loop. If the array was array=[[1,2,3],[7,8,9],[4,5,6]] .. the index your code gives is still 1
0

No need for a counter variable really. I would go for:

for inner_array in array:
  if inner_array.index(5) > -1:
    return array.index(inner_array);
return None;

2 Comments

you may want to return None, instead of -1; as -1 is also a valid index. :)
what if 5 was in index 0 of the inner array?
0

I see functional examples, so I'm a bit late:

[(id,inner.index(5)) for id,inner 
 in enumerate(arr) 
 if 5 in inner]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.