1

Let's say I have the following list

List = ['Apple', 'Banana', 'Orange', 'Grapes']

From this I want to search Apple I have to use the following code

if 'Apple' in List: 
    print "Found"

But I want to search the string which contains 'App' what do I have to do?

I can use For loop and if statement combined.

for items in List: 
    if 'App' in items: 
        print "Found"

But is there any other way to do this process?

user3707514
  • 45
  • 1
  • 5

3 Answers3

2

You can use map + any:

lst = ['apple', 'banana']

if any(map(lambda x: 'app' in x.lower(), lst)):
    print "Found"

Slightly more pythonic:

lst = ['apple', 'banana']
if any(['app' in x.lower() for x in lst]):
    print "Found"

A slightly more optimized version using a generator (if the first element is "apple", it won't check the rest of the lst):

generator = ('app' in x.lower() for x in lst)
if any(generator):
    print "Found"
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • What is a generator? that syntax looks like a n-tuple to me. – kpie May 11 '15 at 03:59
  • @kpie: Start at [Iterators](https://docs.python.org/3/tutorial/classes.html#iterators) in the tutorial, and read through the end of the chapter. – abarnert May 11 '15 at 04:02
  • http://stackoverflow.com/questions/1756096/understanding-generators-in-python - essentially it's a lazy way of iterating over the list. Instead doing all the work immediately, it will only do work on the current element while iterating. – Martin Konecny May 11 '15 at 04:02
  • Not quite pythonic`any(['app' in x.lower() for x in list])` always creates a full list every time, also `list` is probably not a great name for a variable. `if any('app' in x.lower() for x in lst)` would be pythonic – Padraic Cunningham May 17 '15 at 11:03
  • Hey @PadraicCunningham, thanks for the feedback - changed the var name of `list`, for your suggested code, it's actually in the answer at the end. – Martin Konecny May 17 '15 at 14:34
0
if(True in ["app" in k for k in ["apple","orange","grapes"]]):
    print("woot")

Is more or less readable but does the same thing.

you could even make it a one liner,

print "woot" if(True in ["app" in k for k in ["apple","orange","grapes"]]) else ""
kpie
  • 9,588
  • 5
  • 28
  • 50
0

If you're just looking for something more concise, the exact same logic can be written with a comprehension (in this case, a generator expression) and the any function:

if any('App' in item for item in List):
    print "Found"

If it isn't obvious why this does almost the same thing as your existing code:

If you expand the comprehension to an explicit for statement, it looks like this:

for item in List:
    yield 'App' in item

Then, the any function iterates over each thing that got yielded until one of them is true, or until it gets to the end and they were all false.

So, the only difference is that instead of printing "Found" once for each time something was found, it just prints it once and then stops looking.


If you want something more efficient, you'd need to change your data structure. But I doubt that's an issue when you've only got 4 things in List.

abarnert
  • 354,177
  • 51
  • 601
  • 671