6

I'm trying to do the following logic:

bitmap_id = 'HelloWorld'
if 'SLIDE' not in bitmap_id and 'ICON' not in bitmap_id  and 'BOT' not in bitmap_id:
    print bitmap_id

So if bitmap_id is 'ICON_helloworld', then nothing should be printed.

I'm pretty sure you all agree that it's too long and looks ugly, so I tried to do it as shown below, but it's not working.

if any(s not in bitmap_id for s in ['SLIDE', 'ICON', 'BOT']):
    print bitmap_id

What am I doing wrong?

3
  • Have you tried declaring the table out of the command line, and just make a for loop for each string in the table ? Commented Aug 5, 2015 at 8:29
  • 1
    It looks to me like in your first statement you are using 'and' but in your second statement you have an implicit 'or'. Commented Aug 5, 2015 at 8:30
  • @ypnos thanks heaps for hint, I'm new to Python and didn't know of the all function :) your comment was sufficient for me to find the answer quickly Commented Aug 5, 2015 at 8:35

3 Answers 3

15

Use either

if all(s not in bitmap_id for s in ['SLIDE', 'ICON', 'BOT']):
     print bitmap_id

or

if not any(s in bitmap_id for s in ['SLIDE', 'ICON', 'BOT']):
    print bitmap_id
Sign up to request clarification or add additional context in comments.

Comments

3

You need actually:

if all(s not in bitmap_id for s in ['SLIDE', 'ICON', 'BOT']):
    print bitmap_id

if any will be false only if all of the of the strings are in bitmap_id, for example 'bitmap_id = 'ICON_SLIDE_BOT'. You want opposite, that none of the strings are there or all strings are not there.

2 Comments

you got the answer over DeepSpace by one minute! Sorry for the late acceptance! actually forgot bout this question as you guys answered it so fast I had to wait to accept! cheers,
@sayo9394 I actually like his answer more, he has 2 options.
2

You can do something like this:

bitmap_id = 'HelloWorld'
blacklist = ['SLIDE', 'ICON', 'BOT']
if not filter(lambda x: x in bitmap_id, blacklist):
    print bitmap_id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.