Python Forum
"Up to but not including" (My personal guide on slicing & indexing)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Up to but not including" (My personal guide on slicing & indexing)
#5
@ichabod801: Yes, that was a typo. Thank you for catching that. And thanks for the clarification using your lunch list example. This is enormously helpful.

@micseydel: Your sample function which returns the length of a list in a given range (n) proves that it matches the function's initial n parameter. I really like this. Thank you for sharing.

I played around some more with slicing and such. I’ve pushed my changes back up to my GitHub repo here.

Here is my take way based on what I have learned here:

One way to distinguish between len() and the integers passed into a list slice is to reflect on the following code snippet:

colours5 = ["indigo", "orange", "crimson","emerald",]

# To get the last index of len of list, this will not work:
print(f'First attempt: {colours5[len(colours5):]}') 

# To get the last index of len of list...
# ...you need to subtract 1 integer from the total length of the list:
print(f'Second attempt:{colours5[len(colours5)-1:]}') 
Here is the output:

Quote:First attempt: []
Second attempt:['emerald']

The final item in the list above is the 4th. But submitting '4' as a slice returns nothing. To grab the last item in a list, you need to take the total length minus 1. This distinction between len() and slicing parameters is a helpful way of recalling the difference between the two. This is clear and easy to remember.

Referring back to the code sample in my original post (here it is again):

import random
new_list = [ random.randint(0,5) for i in range(0,5) ]
print(new_list)
Here is the output:

Quote:[3, 4, 1, 4, 0]

From this code snippet, as you can see, if there is a single integer passed into the range() function, it starts at 0 and goes up to but does not include the final number in the generated list. In this way it's similar to list slicing. But when passing in parameters into randint(), the result includes any integer from the beginning parameter as well as up to (and including) the final integer at the end. This isn't quite as easy to remember. Next time I get disoriented when writing my script in the future, I'll refer back to this written distinction.
Reply


Messages In This Thread
RE: "Up to but not including" (My personal guide on slicing & indexing) - by Drone4four - Nov-20-2019, 09:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Function to count words in a list up to and including Sam Oldman45 15 6,748 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Complete Guide to Shodan Calli 4 2,355 Jul-18-2022, 04:29 PM
Last Post: Calli
  Including data files in a package ChrisOfBristol 4 2,601 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,059 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,948 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  slicing and indexing a list example leodavinci1990 4 2,385 Oct-12-2020, 06:39 AM
Last Post: bowlofred
  Need to access Windows Machine or Personal Certificate Store and send to web app seswho 0 1,662 Sep-14-2020, 04:57 PM
Last Post: seswho
  New User online Guide NAP1947 1 1,576 Sep-06-2020, 04:36 PM
Last Post: Larz60+
  Including modules in Python using sys.path.append JoeDainton123 1 2,946 Aug-24-2020, 04:51 AM
Last Post: millpond
  Including a Variable In the HTML Tags When Sending An Email JoeDainton123 0 1,910 Aug-08-2020, 03:11 AM
Last Post: JoeDainton123

Forum Jump:

User Panel Messages

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