1

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
1
  • Does the any() function help? Commented Aug 25, 2021 at 20:20

4 Answers 4

3

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"))
Sign up to request clarification or add additional context in comments.

2 Comments

This is definitely a great answer, but it wouldn't return 0 if print(function(lst,"Carrot")) is entered.
yes, let me edit it and i will make it check if the word is in the word that in the list
1

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')

Comments

1

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.