how can I get the index of the item in a list that contains the string being search (within the list) in python

Bettina SB

I'm looking for a string within a list of strings and the string to be found is a subset of one of the strings in the "search" list I need a way to get in which item of the list was the string found example:

mylist = ['hola pepe', 'hola manola', 'hola julian', 'holasofi']

searchitem1 = 'pepe'

searchitem2 = 'sofi'

I tried:

     mylist.index('pepe')

but didn't work (as its not the exact string I guess?) I also tried:

    if any(ext in 'pepe' for ext in mylist): mylist.index(ext)

but didn't work either.... What i'm looking at Is like stopping when the string is found and getting from the code in which item the find occurred....

thanks!

progmatico

If you are new to Python, maybe you are better by now with the classical approach in Chris answer. But if you feel curious about generators see the following example using a generator expression (essentially the same as Chris answer but more compact):

>>> mylist = ['hola pepe', 'hola manola', 'hola julian', 'holasofi', 'oi pepe']
>>> gen = ((index,elem) for index, elem in enumerate(mylist) if 'pepe' in elem)
>>> next(gen)
(0, 'hola pepe')
>>> next(gen)
(4, 'oi pepe')
>>> next(gen)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>>

The indexes can be extracted as usual like in

>>> next(gen)[0]

A generator object is always created even if there are no elements that match the if clause in the generator expression.

To handle the exception when next() does not yield more values use a try block to capture the StopIteration:

search = 'pepe'
...
try:
    next_result = next(gen)

except StopIteration:
    print('No more matches for {}'.format(search))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get the index of a string in Python list, which contains a certain character?

Python: How can I check if an item in a list contains a string within an elif statement where two conditions must be met?

How to test if a string contains an item within a list?

How do I use .index to search for an item's position in list within a list?

How can I get the index of a nested list item?

Way to check if target string contains any item in list and get the index? | Python3

How to get index and value of an item in python list?

How i can i traverse a list and check if an equal item contains there?

How can I replace a text delimited string list item with multiple list items in a python list?

Get item index of a list within Select statement

How can I get the list of all the items nested(hierarchy) within the item based on parent-child relationship?

How i can get GameObject index in the list?

python how can I print the item in a list?

How can I see if a list contains another list in Python?

How to use startsWith and contains within a List of String

How to loop through each row within a dataframe that contains a string, and match such string to each item in a list?

How to get List index of tapped List item?

How can I get python to output just string out of a list?

How can I append a string item as a sublist item in a list?

How can I substitute a list by index in python?

How can I get the first and last item of every item of list

How to get index from List<string> of specific item?

How to check if an item exists in a string List and if it exists, get its index

How can I get the longest string that contains the most occurrences of a character in a list

How can I get an index of an outer list element in a list of lists?

how can I select 1 item by index the list in a generator result?

How to delete a item in list based on partial search string in python?

How can I check a string contains all values in a list?

How can I get the list of ldap users without being sudo?

TOP Ranking

HotTag

Archive