-1

So say I have

list = ['I am blue', 'I am red', 'I am not blue', 'I am green']

Is there a way to create a new list such that it will contain only the entries in "list" that contain the word 'blue' (and disregard the other parts of that string)? i.e.

blue_list = ['I am blue', 'I am not blue']

and then I'd need to check the amt of entries in "blue_list" but I can do that with

len(blue_list)

Thanks!

vaultah
  • 44,105
  • 12
  • 114
  • 143
Nick
  • 27
  • 5
  • String containment and word containment are very different problems. "blue" is a part of the word "abluent", for example: do you want "the abluent was effective" to be removed or not? – DSM Mar 29 '16 at 16:56

1 Answers1

0

Use a list comprehension, while you're at it rename your list so that it doesn't shadow the built in:

l = ['I am blue', 'I am red', 'I am not blue', 'I am green']
blue_list = [phrase for phrase in l if 'blue' in phrase]
Bahrom
  • 4,752
  • 32
  • 41