1

I have the following set-up:

stuff = ['apple', 'I like apples today', 'orange', 'oranges and apples guys']

What I want to do: I want to search each value in a list to see if the word "orange" is contained anywhere within that lists index value.

In other words, my expected output is this:

orange
oranges and apples guys

What I am currently getting back is nothing.

Here is what I am currently doing:

for x in range(0, len(stuff), 1):

     if 'orange' in stuff[x] == True:
          print('testing to see if this works')

I am not successful with this, any suggestions?

Edit #1:

I tried searching for the contains syntax to use with the re module, but did not return anything useful.

Edit #2:

What about for cases like this:

stuff = ['crabapple']

'apple' in stuff

False

The word "apple" does exist, it's just part of another word. In this case, I'd like to return that as well.

  • 1
    What happens if you add some parentheses around the `('orange' in stuff[x])`? – wim Sep 06 '18 at 02:30
  • the `in` operator already returns boolean, you don't need `== True` – Kevin Fang Sep 06 '18 at 02:31
  • Wim: Your solution worked, I'm going to guess that's because without the parenthesis, it was looking to see if "stuff" at index value x, literally equaled true, would you say that is correct? – Chicken Sandwich No Pickles Sep 06 '18 at 02:32
  • 1
    I would say that's partly correct. Your problem is actually due to an unexpected chained comparison, but I suspect closing immediately as a duplicate wouldn't help you very much. Here is an example answer from Tim Peters: https://stackoverflow.com/a/45180967/674039 – wim Sep 06 '18 at 02:50
  • Related open issue about changing the behaviour in Python directly, since it trips up new users relatively often: https://bugs.python.org/issue32055 *Reconsider comparison chaining for containment tests* – wim Sep 06 '18 at 03:04
  • I'm on a bit of a time crunch, so I'll have to check that out later. I did edit my post though, edit #2, if you have a second? – Chicken Sandwich No Pickles Sep 06 '18 at 04:42

4 Answers4

3

Use list comprehension

print ([x for x in stuff if "orange" in x])
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
PurushothamC
  • 111
  • 8
0

The way you use in is not correct, you should remove ==True, or python will treat it as 'orange' in a binary list.

for x in stuff:
    if 'orange' in x:
        print('testing to see if this works\n')
        print(x)

testing to see if this works

orange
testing to see if this works

oranges and apples guys
smartass
  • 26
  • 4
0

use this:

for x in range(0, len(stuff), 1):
    if ('orange' in stuff[x])== True:
        print('testing to see if this works')

for your code, python will judge 'stuff[x] == True' (and it will be False) then judge 'orange in False' so it will always be False.

Jerry Bai
  • 41
  • 2
  • 3
0

The find method of strings returns the index of the substring, or -1 if not found:

for s in stuff:
    if s.find('orange') != -1:
        print(s)
tigerninjaman
  • 393
  • 3
  • 17