[Tutor] creating dictionary from a list

Saad Javed sbjaved at gmail.com
Sat Apr 13 20:19:58 CEST 2013


>
> Don't fight Python, unlike this chap[1] :)  Basically if you're looping
> around any data structure you rarely need to use indexing, so try this
> approach.
>
> for item in lst:
>     if item.startswith(('Mon','Tue','**Wed','Thu','Fri','Sat','Sun'))**:
>         myDict[item] = []
>         saveItem = item
>     else:
>         myDict[saveItem].append(item.**strip())
>

This creates a dictionary whose keys are out of order in terms of dates.

{'Thu Apr 04': ['Weigh In'], 'Sat Apr 06': ['Collect NIC', 'Finish PTI
Video'], 'Wed Apr 10': ['Serum uric acid test'], 'Sun Apr 14': ['Download
Louis CK Oh My God', '4:00pm', 'UPS Guy'], 'Sat Apr 13': ['1:00pm', 'Get
flag from dhariwal']}

Sat Apr 13 is appearing after Sun Apr 14. How do you sort this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130413/579ef478/attachment-0001.html>


More information about the Tutor mailing list