-4
['aa', 'ab', 'aaaa', 'ggg', 'agaga', 'a']

From the above list, I'd like to be able to find out if the list contains a 'b' in the shortest possible ways. Thanks!

Charlie
  • 21
  • 6

2 Answers2

7

For substrings:

any('b' in s for s in input_list)

For full strings:

'b' in input_list
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
1

You can join the items an test for a b in the joined string:

'b' in ' '.join(your_list)
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • This works well enough if you're checking for a string of length one. But if you're looking for `"xyz"`, and `your_list` contains `['ax', 'yz']`, your expression returns True even though no single element of `your_list` contains "xyz" – Kevin Mar 04 '15 at 16:58
  • Then make the separator a space. – Malik Brahimi Mar 04 '15 at 16:59
  • Then it will incorrectly give True if you're looking for `"x yz"`. – Kevin Mar 04 '15 at 17:27