Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Functional Python Functional Workhorses attrgetter

Callum Anderson
seal-mask
.a{fill-rule:evenodd;}techdegree
Callum Anderson
Data Analysis Techdegree Student 10,200 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.

Thanks

sorting.py
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)))
print(sorting_dates)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 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!!