I just found this code:

def get_product_item(jsonld_items):
    for item in jsonld_items:
        if item['@type'] == 'Product':
            return item
    else:
        return {}


My argument is that the intent is clearer in:

def get_product_item(jsonld_items):
    return first((item for item in jsonld_items if item['@type'] ==
'Product'), {})


As a reminder, first()'s definition in Python is:

def first(seq, default=None):
    return next(iter(seq), default=default)


It could be optimized (implemented in C) if it makes it into the stdlib.

-- 
Juancarlo *Añez*
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TVFD2RNRAU3SLHGMNQPBO4DHWFT274W3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to