Reputation: 27
Suppose I have the following list with only strings in it.
appliances = ['blender', 'microwave', 'oven', 'toaster']
How do I generate a new list that is composed of the elements of the list appliance, that contains only the strings that have the letter v in it? After running it through, the new list looks like this:
short = ['oven', 'microwave']
Upvotes: 2
Views: 93
Reputation: 19981
The tools you need are list comprehensions (http://docs.python.org/tutorial/datastructures.html#list-comprehensions) and the in
operator (http://docs.python.org/reference/expressions.html#in).
Upvotes: 1
Reputation: 236122
Try this:
short = [x for x in appliances if 'v' in x]
Upvotes: 6