Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists + Empty Lists
#1
Sorry if this is stupid but I have created a list of days

days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']

I am trying to assign the weekdays to an empty list:
weekdays = []

and the weekend days to an empty list:
weekends = []

I can't figure out how to assign the days properly, would I use a while loops or a for loop? Or would I need to loop them at all?
Sorry again if it's a stupid question.
Reply
#2
days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
weekdays = days[:5]
weekends = days[5:]

print(weekdays)
print(weekends)
Output:
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] ['Saturday', 'Sunday']
Reply
#3
(Mar-22-2021, 06:47 PM)Axel_Erfurt Wrote:
days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
weekdays = days[:5]
weekends = days[5:]

print(weekdays)
print(weekends)
Output:
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] ['Saturday', 'Sunday']

Hey Axel,

Thanks so much but does this mean I'm assigning the days to empty lists or am I just printing the first five and then anything after the first five?

Thanks again
Reply
#4
They are not empty

weekdays is a new list with the week days
weekends is a new list with the weekend days
Pytho13 likes this post
Reply
#5
(Mar-22-2021, 06:52 PM)Axel_Erfurt Wrote: They are not empty

weekdays is a new list with the week days
weekends is a new list with the weekend days

I see thank you, do you know a way to assign the days to two separate empty lists?
Reply
#6
Hint:
>>> import calendar
>>> for num, day in enumerate(calendar.day_name):
...     print(num, day)
... 
0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday
>>>
Pytho13 likes this post
Reply
#7
(Mar-22-2021, 06:59 PM)Larz60+ Wrote: Hint:
>>> import calendar
>>> for num, day in enumerate(calendar.day_name):
...     print(num, day)
... 
0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday
>>>

Hi, Thanks so much for getting back to me.
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?

It's how to assign it that I'm mainly having trouble with.

Sorry :(
Reply
#8
Would something like this work:

days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']

#Create empty lists for weekdays and weekends
weekdays = []
weekends = []


index = 0
while index < len(days):
day = days[index]
print( days )
index = index + 1
Larz60+ write Mar-23-2021, 12:17 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#9
You have a list with all days and you want 2 lists with weekdays and weekend days ?

I wrote it before

weekdays = days[:5]
weekends = days[5:]
Reply
#10
(Mar-22-2021, 07:40 PM)Axel_Erfurt Wrote: You have a list with all days and you want 2 lists with weekdays and weekend days ?

I wrote it before

weekdays = days[:5]
weekends = days[5:]

Thank you again for replying, this is what I'm trying to do:

Create a list containing the days of the week from Monday to Sunday and two empty lists weekday = [ ] and weekend = [ ]. Allocate each element from the list containing the days to the appropriate list: either weekday or weekend.

Print the weekdays from the weekday list in the order from Monday to Friday.
Print the weekend days from the weekend list in the order Saturday, Sunday.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Better way to append frames from a video to multiple lists? Balaganesh 0 218 May-13-2021, 07:37 AM
Last Post: Balaganesh
  List of lists - merge sublists with common elements medatib531 1 332 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 354 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  ''.join and start:stop:step notation for lists ringgeest11 1 331 Apr-08-2021, 04:29 PM
Last Post: BashBedlam
  lists JimLake 10 767 Apr-06-2021, 01:50 PM
Last Post: jefsummers
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 561 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Formatting lists rturus 5 677 Nov-26-2020, 04:53 PM
Last Post: bowlofred
Star --- Python lists and Linked Lists --- sabe 3 853 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  Noob question about lists adifrank 4 730 Nov-19-2020, 03:26 AM
Last Post: adifrank
  Creating list of lists from generator object t4keheart 1 475 Nov-13-2020, 04:59 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020