I like it when I find one liner that make the code better and and faster to understand.

The worst one liners are those that force you to twist you mind in weird ways in order to understand them.

A simple one, utilizing the bool return value from the in” operator

def is_wanted(self, item):
    if item in self.wanted_items:
        return True
    return False
def is_wanted(self, item):
    return item in self.wanted_items

Using list comprehension and the any” built in function

def is_wanted(self, item):
    for type in self.wanted_types:
        if item.upper().startswith(type):
            return True
        else:
            return False
def is_wanted_type(self, item):
    return any([item.upper().startswith(type) for type in self.wanted_types])

January 1, 2015


Previous post
Xcode Beginner Shortcuts I just started to learn Objective C and here are some Xcode 5 shortcuts that I find useful when just getting started with the Editor. Move cursor
Next post
With Python pick random item If you have a list of items or a sequence and you want to pic a random item from said sequence. Here is a quick way to do it