Given the list ['a','ab','abc','bac'], I want to compute a list with strings that have 'ab' in them. I.e. the result is ['ab','abc']. How can this be done in Python?
you win by 48 seconds :-)
Eli Bendersky
2010-01-28 07:20:34
+2
A:
Tried this out quickly in the interactive shell:
>>> l = ['a', 'ab', 'abc', 'bac']
>>> [x for x in l if 'ab' in x]
['ab', 'abc']
>>>
Why does this work? Because the in operator is defined for strings to mean: "is substring of".
Also, you might want to consider writing out the loop as opposed to using the list comprehension syntax used above:
l = ['a', 'ab', 'abc', 'bac']
result = []
for s in l:
if 'ab' in s:
result.append(s)
Daren Thomas
2010-01-28 07:19:53
+2
A:
This simple filtering can be achieved in many ways with Python. The best approach is to use "list comprehensions" as follows:
>>> lst = ['a', 'ab', 'abc', 'bac']
>>> res = [k for k in lst if 'ab' in k]
>>> res
['ab', 'abc']
>>>
Another way is to use the filter function:
>>> filter(lambda k: 'ab' in k, lst)
['ab', 'abc']
>>>
Eli Bendersky
2010-01-28 07:20:01
@S.Lott: why? What's wrong with learning useful advanced programming topics in a suitable context?
Eli Bendersky
2010-01-28 12:23:08
@Edi Bendersky: Because they're more confusing than useful? Because it leads to questions that are answered by "use a def"? Because it rarely leads to anything better than code golf? I don't know, they seem useless to me, after 30 years of programming in a wide variety of languages. But I guess they're really important to you.
S.Lott
2010-01-28 12:33:19
@S.Lott: I think lambdas are facilitating the consideration of functions as first-class objects, which is important for some programming paradigms. I wouldn't say they're *very important to me*, but I believe even newbies can benefit from thinking about programming this way, and definitely wouldn't call it *inflicting*.
Eli Bendersky
2010-01-28 13:19:30
@Eli Bendersky: I'm guessing they're important because you don't have a lot of reasons for using them. Considering that SO has more questions on `lambda` than on `def`, I'm guessing that lambda really *is* confusing and hard to understand. I'm not sure that a lambda helps much because it's the `filter` function that forces one to think about functional programming. Any example with `filter` (like this one) is good stuff for showing functional programming. Omitting the lambda -- IMO -- makes it an even better way to show the power of functional programming.
S.Lott
2010-01-28 13:55:53
@S.Lott: but isn't `lambda` the perfect companion to `filter` in this case? I think that writing a separate function just for checking if `ab` is in the given list is an overkill. So is writing a more general function that basically wraps the `in` operator. How would you use `filter` in a clearer way without `lambda` here?
Eli Bendersky
2010-01-28 14:06:00
@Eli Bendersky: No. I find that lambda is not the *perfect* companion to much of anything. I would use `def` and I would find it less confusing; I would probably have to answer one fewer question on lambda. And the answer wouldn't look like code golf. As I said, the `filter` is good. I have found that a simple `def` will not harm the importance of n00bs learning how `filter` works.
S.Lott
2010-01-28 18:22:51
A:
list = ['a', 'ab', 'abc', 'bac']
prefix = 'ab'
filter(lambda x: x.startsWith(prefix), list)
Itay
2010-01-28 07:22:48