How should I write this string-prefix check so that it's idiomatic Python?

Posted by Kevin Stargel on Stack Overflow See other posts from Stack Overflow or by Kevin Stargel
Published on 2010-04-07T14:59:53Z Indexed on 2010/04/07 15:23 UTC
Read the original article Hit count: 258

Filed under:
|
|

I have a couple of lists of items:

specials = ['apple', 'banana', 'cherry', ...]
smoothies = ['banana-apple', 'mocha mango', ...]

I want to make a new list, special_smoothies, consisting of elements in smoothies that start with the elements in specials. However, if specials is blank, special_smoothies should be identical to smoothies.

What's the most Pythonic way to do this? Is there a way to do this without a separate conditional check on whether specials is blank?

© Stack Overflow or respective owner

Related posts about python

Related posts about lists