vossmalte
vossmalte

Reputation: 184

How do i get index of list in list?

list = ["word1", "word2", "word3"]
print list.index("word1")

this works fine!

but how do i get index of this:

list = [["word1", "word2", "word3"],["word4", "word5", "word6"]]
print list.index("word4")

well that doesnt work, error:

ValueError: "word4" is not in list

i expect an answer like 1,0

Upvotes: 5

Views: 12614

Answers (5)

Carlos
Carlos

Reputation: 2232

In the future, try to avoid naming your variable list, since it will override Python's built-in list.

lst = [["word1", "word2", "word3"],["word4", "word5", "word6"]]

def find_index_of(lst, value):
   for index, word in enumerate(lst):
      try:
        inner_index = word.index(value)
        return (index, inner_index)
      except ValueError:
        pass
   return ()

This loops through each element of lst and it will:

  • Attempt to get the index of the value. If we have found the element, then let's return the indexes.
  • However, if Python throws a ValueError (because the element is not on the list), then let's continue with the next list.
  • If nothing was found, return an empty tuple.

output:

find_index_of(lst, 'word4') # (1, 0)
find_index_of(lst, 'word6') # (1, 2)
find_index_of(lst, 'word2') # (0, 1)
find_index_of(lst, 'word78') # ()

Upvotes: 1

tobias_k
tobias_k

Reputation: 82889

Try something like this:

def deep_index(lst, w):
    return [(i, sub.index(w)) for (i, sub) in enumerate(lst) if w in sub]

my_list = [["word1", "word2", "word3"],["word4", "word5", "word6"]]
print deep_index(my_list, "word4")
>>> [(1, 0)]

This will return a list of tuples with the first element pointing to the index in the outer list and the second element to the index of the word in that sub-list.

Upvotes: 5

imkost
imkost

Reputation: 8163

def get_index(my_list, value):
    for i, element in enumerate(my_list):
        if value in element:
            return (i, element.index(value))
    return None


my_list= [["word1", "word2", "word3"], ["word4", "word5", "word6"]]
print get_index(my_list, "word4")

prints (1, 0)

Upvotes: 1

shx2
shx2

Reputation: 64298

For multidimensional indexing, assuming your data can be represented as NxM (and not a general list of lists), numpy is very useful (and fast).

import numpy as np
list = [["word1", "word2", "word3"],["word4", "word5", "word6"]]
arr = np.array(list)
(arr == "word4").nonzero()
# output: (array([1]), array([0]))
zip(*((arr == "word4").nonzero()))
# output: [(1, 0)] -- this gives you a list of all the indexes which hold "word4"

Upvotes: 2

thkang
thkang

Reputation: 11543

I think you have to manually find it-

def index_in_list_of_lists(list_of_lists, value):
   for i, lst in enumerate(list_of_lists):
      if value in lst:
         break
   else:
      raise ValueError, "%s not in list_of_lists" %value

   return (i, lst.index(value))


list_of_lists = [["word1", "word2", "word3"],["word4", "word5", "word6"]]
print index_in_list_of_lists(list_of_lists, 'word4') #(1, 0)

Upvotes: 1

Related Questions