Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists + Empty Lists
#11
days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
weekdays = []
weekends = []

print("weekdays:")
for x in range(5):
    print(days[x])
    weekdays.append(days[x])

print("\nweekend:")
for x in range(5, len(days)):
    print(days[x])
    weekends.append(days[x])
shorter

days = ['Monday','Tuesday', 'Wednesday', 'Thursday','Friday', 'Saturday', 'Sunday']
weekdays = days[:5]
weekends = days[5:]

print("weekdays:") 
print("\n".join(weekdays))
print("\nweekends:")
print("\n".join(weekends))
result on both

Output:
weekdays: Monday Tuesday Wednesday Thursday Friday weekends: Saturday Sunday
Reply
#12
What Axel_Erfurt is doing is called slices, and it is how you should extract "slices" from a list. A slice is like this: the_list[start:end]. The slice starts with the_list[start] and ends at the_list[end-1].

If you leave out the start, i.e. the_list[:end], the slice includes all values starting with the_list[0] and ending with the_list[end-1]. If you leave out the end, i.e. the_list[start:], the slice includes all values starting at the_list[start] and ending at the end of the list.

It is a common belief among new Python programmers that print() can do things like return values or assign values. This is about as incorrect as you can be. All print(str) does is write str to a stream called stdout (standard output). Usually this appears in the terminal where you launched the Python program, but it can be redirected to appear in a different window or a file or a pipe.

print(days) does not add anything to a list or remove anything from a list. print(days) doesn't even write days to stdout. It writes a string what some programmer thought fairly represents days.

If you wanted to use a list to copy values from days to weekdays it would look something like this:
weekdays = []
weekends = []
for i in range(5):
    weekdays.append(day[i])
for i in range(5, 7):
    weekends.append(day[i])
Just looking at how ugly this code is, it is easy to understand why slices were added to the language.
Reply
#13
(Mar-22-2021, 08:49 PM)deanhystad Wrote: What Axel_Erfurt is doing is called slices, and it is how you should extract "slices" from a list. A slice is like this: the_list[start:end]. The slice starts with the_list[start] and ends at the_list[end-1].

If you leave out the start, i.e. the_list[:end], the slice includes all values starting with the_list[0] and ending with the_list[end-1]. If you leave out the end, i.e. the_list[start:], the slice includes all values starting at the_list[start] and ending at the end of the list.

It is a common belief among new Python programmers that print() can do things like return values or assign values. This is about as incorrect as you can be. All print(str) does is write str to a stream called stdout (standard output). Usually this appears in the terminal where you launched the Python program, but it can be redirected to appear in a different window or a file or a pipe.

print(days) does not add anything to a list or remove anything from a list. print(days) doesn't even write days to stdout. It writes a string what some programmer thought fairly represents days.

If you wanted to use a list to copy values from days to weekdays it would look something like this:
weekdays = []
weekends = []
for i in range(5):
    weekdays.append(day[i])
for i in range(5, 7):
    weekends.append(day[i])
Just looking at how ugly this code is, it is easy to understand why slices were added to the language.

Hi, thank you so much for explaining this. You are right, I'm new to python and finding parts tricky to understand. I was of the impression the I would need to assign from one list to the other two and this is what I was typing into Google for assistance so I really appreciate you taking the time to explain this.

Have a great day :)
Reply
#14
(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?

It's how to assign it that I'm mainly having trouble with.
Can write some code where look at enumerate days and making a dictionary.
>>> 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'}
Reply
#15
Quote:Can write some code where look at enumerate days and making a dictionary.
suggested top one in post 7
Reply
#16
Hello Pytho13!! Smile Blush ,

I hope you are well. I know how to solve this problem!

Make a for loop, with the i starting from 1 and ending to 4. Code: for i in range(5):
And inside the loop, put days[i] into weekdays. If you don't understand what I mean,
I mean like when the i value is zero you put the 1st item of "days"(Monday) into the list "weekdays".
By the way here is the code to do that:
for i in range(5):
weekdays.append(days[i])
It will go on until it reaches Saturday.
Now for the next list "weekends".
Make another for loop with i starting from 5 to 6 because days[5] and days[6] are the weekend days.
Code for the for loop: for i in range(5, 7, 1):
Next you need to actually put the weekdays into the "weekends" list.
Now that you have got your for loop, we need to put something inside it!
It's pretty much the same code last time except a little different.
The i starts at 5, and days[5] is Saturday. We need to put Saturday into the "weekends" list.
And the i ends at 6, and days[6] is Sunday. We also need to put Sunday into the "weekends" list.
So the code we need to put inside this for loop is:
weekends.append(days[i])
When the for loop starts, the i will be 5. weekends.append(days[5]) that code will append Saturday into the "weekends" list. When the for loop ends, the i will be 6. weekends.append(days[6]) that code will append Sunday into the "weekends" list.
Hope I was able to solve the problem for you! Sorry this was a long message, I was just trying to explain the easiest I could. Wink If you need any more help on anything please contact me! Have a great day.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print lists MarekGwozdz 4 613 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,436 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 694 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 699 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 767 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,381 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 712 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,232 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 732 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