itemgetter instead of def/lambda
Closed this issue · 1 comments
gabrielsroka commented
Hi
thanks for offering this course online. i've learned a ton!
this example:
def stock_name(s):
return s['name']
portfolio.sort(key=stock_name)
can also be implemented using itemgetter
:
from operator import itemgetter
portfolio.sort(key=itemgetter('name'))
see https://docs.python.org/3/howto/sorting.html#operator-module-functions
and https://docs.python.org/3/library/operator.html#operator.itemgetter
i know it kinda defeats the purpose of what you're trying to show, but it's an alternative to keep you from writing one-liners.
maybe include both?
dabeaz commented
There's only so much information one can put in a course before heads explode. Lambda is a more general (and important) idea than using itemgetter.