Mar-23-2021, 01:09 PM
(Mar-22-2021, 07:07 PM)Pytho13 Wrote: Would I be right in thinking that I would need to print number 0-4 and assign them to the first empty list and then assign the 5-6 to the second empty list?Can write some code where look at enumerate days and making a dictionary.
It's how to assign it that I'm mainly having trouble with.
>>> days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday'] >>> week = {} >>> for index, day in enumerate(days, 1): ... week[index] = day ... >>> week {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'} >>> week[6] 'Saturday'Look at slicing out when it's a dictionary.
>>> weekends_day = {6, 7} >>> {key:week[key] for key in weekends_day if key in week} {6: 'Saturday', 7: 'Sunday'}Or using itertools.islice which is clean way to set from to end day.
>>> import itertools >>> >>> weekdays = dict(itertools.islice(week.items(), 0, 5)) >>> weekends = dict(itertools.islice(week.items(), 5, 7)) >>> >>> weekdays {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday'} >>> weekends {6: 'Saturday', 7: 'Sunday'}