Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Functional Python Functional Workhorses attrgetter

Callum Anderson
Callum Anderson
9,699 Points

Curious of an alternate way of completing the challenge

I understand that with code challenges that its one way or no way from what it wants but really curious to see if this would work or why it doesn't. Code below.


import datetime
from operator import attrgetter

date_list = [
    datetime.datetime(2015, 4, 29, 10, 15, 39),
    datetime.datetime(2006, 8, 15, 14, 59, 2),
    datetime.datetime(1981, 5, 16, 2, 10, 42),
    datetime.datetime(2012, 8, 9, 14, 59, 2),

sorting_dates = sorted(date_list, key=attrgetter(datetime.strptime(date_list, %d)))

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 67,750 Points

Interesting approach. Perhaps you meant to use strftime which produce a str from a datetime object instead of strptime which produces a datetime object by parsing a string according to a format template. The argument to key must be a callable function that produces a comparable object ("a > b", etc.) so it can be applied to each item in the sorted container. Using strftime and a lambda function passes the challenge:

Also, challenge looking for sorted_dates not sorting_dates.

sorted_dates = sorted(date_list, key=lambda x: str(attrgetter(x.strftime("%d"))))

Post back if you have more questions. Good luck!!