Tea-F-Tea
Tea-F-Tea

Reputation: 123

Python get index of list in list of lists that contains a substring

I have a list that looks like this:

lst = [['Apple Pie', 'Carrot Cake'], ['Steak', 'Chicken']]

I would like to find the index of the list that contains the word ' Carrot', so in this case, the output should be 0

I have tried to make a function

def find_index(l, c):
    for i, v in enumerate(l):
        if c in v:
            return i

res = find_index(lst, 'Carrot')

but this does not work since it looks for items in the list.

Can anyone help me to retreive the index of the list that contains 'Carrot'?

Thanks!

Edit: I received multiple answers for this question, and they all worked. so I decided to time them and accept the fastest one as the answer. here are the results:

import timeit

lst = [['Apple Pie', 'Carrot Cake'], ['Steak', 'Chicken']]


def find_index(l,c):
    for i,v in enumerate(l):
        for j in v:
            if c in j:
                return i

print(timeit.timeit('%s'%(find_index(lst,'Carrot Cake'))))

def find_index(l, c):
    for i, v in enumerate(l):
        if any(c in x for x in v):
            return i

print(timeit.timeit('%s'%(find_index(lst,'Carrot Cake'))))

def function(list_contains,word_to_know):
    for x in list_contains:
        for y in x:
            if word_to_know in y:
                return list_contains.index(x)

print(timeit.timeit('%s'%(function(lst,'Carrot Cake'))))

def index_of_substr(l, substr):
    for i, inner_list in enumerate(l):
        for text in inner_list:
            if substr in text:
                return i

    return -1

print(timeit.timeit('%s'%(function(lst,'Carrot Cake'))))



0.005458000000000001
0.005462599999999998
0.005485600000000004
0.0054621000000000045

Upvotes: 1

Views: 1082

Answers (4)

ronpi
ronpi

Reputation: 490

Well in the question you mention that you know you are only looking at the first level of the list. So in fact you are trying to match a str to a list which will always give you False. You need to make another iteration over each of the inner lists.

In the following code I made a few modifications to make it a bit more readable:

  • names of variables are better to have a meaning, especially when there are multiple variables involved.
  • The return of the function is -1 if no match was found, which is a more common convention than returning None, but of course it is OK to return None as well.
def index_of_substr(l, substr):
    for i, inner_list in enumerate(l):
        for text in inner_list:
            if substr in text:
                return i

    return -1

lst = [['Apple Pie', 'Carrot Cake'], ['Steak', 'Chicken']]
res = index_of_substr(lst, 'Carrot')
print(res)

and the output is 0 of course.

Upvotes: 1

Twfyq Bhyry
Twfyq Bhyry

Reputation: 170

use this function it will print the index

lst = [['Apple Pie', 'Carrot Cake'], ['Steak', 'Chicken']]
def function(list_contains,word_to_know):
    for x in list_contains:
        for y in x:
            if word_to_know in y:
                return list_contains.index(x)
print(function(lst,"Carrot"))

Upvotes: 3

kindall
kindall

Reputation: 184191

You can use any() with a generator expression to determine whether any item in a sublist contains the target string, then return that sublist's index if so.

def find_index(l, c):
    for i, v in enumerate(l):
        if any(c in x for x in v):
            return i

Upvotes: 1

Suneesh Jacob
Suneesh Jacob

Reputation: 806

You can use something like this:

def find_index(l,c):
    for i,v in enumerate(l):
        for j in v:
            if c in j:
                return i
res = find_index(lst, 'Carrot')

Upvotes: 1

Related Questions