Search code examples
pythonpython-3.xrefactoring

Python: how to shorten this loop?


The code seems a bit long for something that trivial.

Is there a pythonic way to shorten it?

fruit = None
for _fruit in basket:
  if _fruit['name'] != 'banana':
    continue
  fruit = _fruit

Solution

  • fruit = None
    for _fruit in basket:
        if _fruit['name'] == 'banana':
            fruit = _fruit