Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists + Empty Lists
#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


Messages In This Thread
Lists + Empty Lists - by Pytho13 - Mar-22-2021, 06:39 PM
RE: Lists + Empty Lists - by Axel_Erfurt - Mar-22-2021, 06:47 PM
RE: Lists + Empty Lists - by Pytho13 - Mar-22-2021, 06:48 PM
RE: Lists + Empty Lists - by Axel_Erfurt - Mar-22-2021, 06:52 PM
RE: Lists + Empty Lists - by Pytho13 - Mar-22-2021, 06:53 PM
RE: Lists + Empty Lists - by Larz60+ - Mar-22-2021, 06:59 PM
RE: Lists + Empty Lists - by Pytho13 - Mar-22-2021, 07:07 PM
RE: Lists + Empty Lists - by Pytho13 - Mar-22-2021, 07:18 PM
RE: Lists + Empty Lists - by Axel_Erfurt - Mar-22-2021, 07:40 PM
RE: Lists + Empty Lists - by Pytho13 - Mar-22-2021, 08:20 PM
RE: Lists + Empty Lists - by Axel_Erfurt - Mar-22-2021, 08:30 PM
RE: Lists + Empty Lists - by deanhystad - Mar-22-2021, 08:49 PM
RE: Lists + Empty Lists - by Pytho13 - Mar-23-2021, 11:40 AM
RE: Lists + Empty Lists - by snippsat - Mar-23-2021, 01:09 PM
RE: Lists + Empty Lists - by Larz60+ - Mar-23-2021, 09:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare lists w_i_k_i_d 6 460 May-23-2024, 07:23 PM
Last Post: deanhystad
Question Using Lists as Dictionary Values bfallert 8 653 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  problem with print lists MarekGwozdz 4 842 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,792 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 962 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 921 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 954 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,556 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 843 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,420 Apr-13-2023, 06:40 AM
Last Post: Dato

Forum Jump:

User Panel Messages

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