Python 使用具有不同过滤条件的列表理解

Python 使用具有不同过滤条件的列表理解,python,Python,有一个函数将参数作为布尔值 def get_choices(retirement): choices = [ { 'id': id, 'name': CommonName.build_item_name( data ) } for cd, data in _collection.items() if not is_used_ite

有一个函数将参数作为布尔值

def get_choices(retirement):
        choices = [
        {
            'id': id,
            'name': CommonName.build_item_name(
                data
            )
        } for cd, data in _collection.items()
        if not is_used_item(cd)
    ]

现在,该函数的工作方式是,其结果不依赖于
retirement
值。是否有可能使列表comp筛选
如果未使用项目(cd)
retirement=True
时不被应用?

添加
是否使用项目(cd)
,因此如果
retirement
True
则条件短路,并且
是否使用项目
不被调用/评估

def get_choices(retirement):
        choices = [
        {
            'id': id,
            'name': CommonName.build_item_name(
                data
            )
        } for cd, data in _collection.items()
        if retirement or not is_used_item(cd)
    ]

添加
报废与否\u项目(cd)
,因此如果
报废
则条件短路且
已使用\u项目
不被调用/评估

def get_choices(retirement):
        choices = [
        {
            'id': id,
            'name': CommonName.build_item_name(
                data
            )
        } for cd, data in _collection.items()
        if retirement or not is_used_item(cd)
    ]