0

I have a list like this but with thousands or maybe millions of elements:

test = [
  ("goodbye", "help", "buy","sell", "1.1.1.1", 25), 
  ("hi dude", "text", "friends","love", "blablabla", 14), 
  ("hi darling", "books", "letter","class", "club", 64)
]

What would be the most optimized code for finding all the elements indexes that contain the word "hi"? In the given example above, it should return test[1] and test[2].

ruohola
  • 21,987
  • 6
  • 62
  • 97
  • there is not much room for optimization here imho. If you need all the indices at once you can use a list-comprehension like `[i for i, x in enumerate(test) if any('hi' in y for y in x)]` – Ma0 Aug 29 '18 at 11:26
  • 3
    Possible duplicate of [Finding the index of an item given a list containing it in Python](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python) – colidyre Aug 29 '18 at 11:27
  • I don't think it is a duplicate of https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python: the `index()` method is not of much help here. – brandizzi Aug 29 '18 at 12:20
  • @Maximiliano just make it work. Don't concern yourself with the performance here so far. Honestly, millions of those elements can probably be processed in less than half a second in any modern machine. Once you have a solution, check _if_ you have performance problem.s _If_ you have, profile your code and then look for alternative solutions. – brandizzi Aug 29 '18 at 12:22
  • @brandizzi there are several answers in the possible duplicate link without using index()-method. – colidyre Aug 29 '18 at 13:12

2 Answers2

1

You can use enumerate and any to get the index of all elements that contains the word 'hi'

>>> test = [("goodbye", "help", "buy","sell", "1.1.1.1", 25), ("hi dude", "text", "friends","love", "blablabla", 14), ("hi darling", "books", "letter","class", "club", 64)]
>>> [i for i,words in enumerate(test) if any('hi' in str(word) for word in words)]
[1, 2]
Sunitha
  • 11,777
  • 2
  • 20
  • 23
0

I believe this is the most optimal;

word = "hi"
matches = []
for tupl in test:
    for elem in tupl:
        if word in elem:
            matches.append(tupl)
            break
ruohola
  • 21,987
  • 6
  • 62
  • 97