Raymond Lam
Raymond Lam

Reputation: 27

List of strings: looking for a particular character

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

Answers (2)

Gareth McCaughan
Gareth McCaughan

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

Óscar López
Óscar López

Reputation: 236122

Try this:

short = [x for x in appliances if 'v' in x]

Upvotes: 6

Related Questions