Friday, April 7, 2017

Lambda vs list comprehension....

Task: Iterate through a list, get json representation of an object and return a list

List comprehension way:

[item.json() for item in ItemObject.query.all()]

Lambda way:

list(map(lambda x: x.json(), ItemObject.query.all()))

It is more pythonic to do it via list comprehension, but people familiar with functional languages might have easier time understanding lambda way

No comments:

Post a Comment