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
  problem with print lists MarekGwozdz 4 612 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,431 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 685 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 695 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 762 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,378 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 711 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,231 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 729 Feb-28-2023, 10:55 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt

Forum Jump:

User Panel Messages

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