Is “end” also a slice notation?

Hi all,

I actually have a question relating to list slicing notation, I think it’s called.

I understand start, stop and step are used, but is “end” also a slice notation? Are these just abstract terms to be used here academically to write a piece of code that, were we to use it, we would replace the notations (start, stop, step, end) with actual indices and step values? I guess not, as they return values, but I can’t seem to find a reference to “end”. I may be conflating some things here. List slicing notations especially are some of the most confusing concepts for me, and any clarification is really appreciated. :slight_smile:

I get the two concepts below, but am not sure if I would replace with values or not, my guess is only step requires a value as in the exercise they return the indices?

a[start:stop:step]

This is clear and simple. I get accessing lists with [ ].

+---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

The only thing that starts to throw me off is when we start to use slicing : combined with notation. I even get the below, it’s clear and makes sense.

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

These are the main questions that arise from the below exercise.

The [:start] - start at the beginning of the array and end at start.
and
The [end+1:] **start from the end plus 1 and go the end of the array. **

Both of these I just simply can’t wrap my head around. <<<

#Write your function here def remove_middle(lst, start, end): return lst[:start] + lst[end+1:] #Uncomment the line below when your function is done print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

cheers for the help folks
Sam

1 Like

The bit I think you’re quoting from mentions some potentially useful information relating to this-

Yes it’s in the strings section but see a few paragraphs down from here https://docs.python.org/3/tutorial/introduction.html#strings:

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

You can probably add on the step defaulting to 1.

So your examples above could be rewritten to-

a[start:stop:]  # items start through stop-1
a[start::]      # items start through the rest of the array
a[:stop:]       # items from the beginning through stop-1
a[::]           # a copy of the whole array

The missing values have defaults and you can skip the extra : symbols when you only need start or start/stop (step needs two, e.g. ::step).

P.S. I’ve seen your other post but it’ll take me time to reply :wink:

1 Like

You are getting the right picture, it would appear. Are we to understand that the positions at both index are to be removed, as well?

1 Like

Hey @tgrtim,

Thanks for the fast reply. I’ll read through it and the links, as always, and try to understand and reply as I research and grasp what you’re saying. I’ve been working in Lists all day with the CC chatbot track, which repeats Python (I did the Python 3 beginner course already) essentially from scratch. Has been very helpful.

No worries’ fella, take your time, just me trying to get things straight in my head. :slight_smile:

Cheers

1 Like

Hi Roy,

Sorry I missed your reply, it wasn’t obvious to me with the way the forum is laid out that you were responding to me.

I’m not entirely sure what you’re asking.

If you’re talking about the codebyte from the exercise, I believe only the elements ?exclusive? of the index notation positions declared are removed I think? Hence, returning [4, 23, 42], sorry if I’m not clear, its this part of the exercise that I’m confused with.

It’s the below that doesn’t seem to conform with anything I’ve been learning up to this point that is spinning me around in circles.

lst[:start] - start at the start of the array through to the :start - I mean, what ?

lst[end+1:] - start at the “end” + 1 (which I have not seen anywhere using a plus operator for index position, only whole or negative) through the :end of the array - confusion mode engaged.

I’m sure Im missing something simple here, I know this isnt rocket science, and driving me nuts that I havent worked it out yet,

Maybe look at it a different way. When I look at photos from space of a body’s surface, my brain a lot of times sees craters as elevated surfaces. It takes a second to see the opposite perspective. What if we remove the middle, literally, rather than piecing together the remnants?

lst[start:end+1] = []
1 Like

Ah, a fellow Astro enthusiast I see, very cool. :telescope:

Thanks for the different perspective, I’ll try and see it that way.

Working list notation out feels more difficult than launching the biggest mirror ever, this month, to orbit around the second Lagrange point 1 million miles away from Earth behind the dark of the moon. :wink:

1 Like

The one biggest difference being that with slices we can make mistakes with zero effect since slices are virtual. JWT needs to get off the Earth flawlessly, and then complete all the steps to deploy the sunshield, open the mirror and latch it into position. Been waiting anxiously this past decade for this telescope to really let us see what is still hidden from Hubble. When that happens… Best Xmas present, ever!

1 Like

I just love the fact you knew instantly what I was referring to mate. Made my day.

Very true, I wonder why then they are so detrimental to my health :sweat_smile:

Couldn’t agree more!! It’s been a tense time these last few years, the delays and potential derailments. I understand some of them were to find further points of failure in post launch deployment. My family/friends have no idea why I’m so excited and anxious this month, as their eyes glaze over when I talk about “observing in the infrared spectrum with such incredible resolution (due to cosmological redshift) allows us to see the earliest”…is basically when I stop talking that and ask about their day. 344 potential points of failure on average with 144 release mechanisms, and they’re wondering why I’m tense, haha. You’re not wrong when you say it needs to be flawless.

I registered online here to watch the launch online with NASA.

In all honesty, I am most excited about exoplanet observation and atmospheric composition detection. Not just for the holy grail, techno/life signatures etc, but also just to know in greater detail what class of planets are out there and in what numbers. Should help refine the Fermi paradox calculations a bit better and maybe give us a better focal range.

I’m just about to buy a Celestron Astro FI beginners telescope and learn how to us it too, make my own observations of transits, and learn how to report on them, I just want to learn everything, all of it. I’d love to work within Astro Physics with Ai/ML hopefully, hence the Ai degree I’m starting next year at JKU. I’m just so ■■■■ excited. What are you most excited about JWST researching?

Ok ok, sorry about all that Roy, that’s enough from me, guess we still need to talk about Python, (although I like that you were helping me learn Python via Astro Physics) back to list notation. :face_with_monocle:

Quasars.

1 Like

The Universe is a giant reactor and ten-plus billion years ago was a lot smaller. Evolutionary steps were a lot closer together. Galaxies were colliding, driven by even greater gravity into clusters where those collisions could take place. Today we’re spread out and can only see what has become of the violence that must have been in the earlier stages.

1 Like

Indeed. Certainly one of the driving forces behind JWST, to look back at that violence, that early red shifted time in the universe and understand it. I’m not as interested in Big Bang Theory myself per se, to be honest, as I am far more interested in Astro Biology, exo planetary biomes (should they exist haha) and planetary bodies. The further discoveries we’ll make via JWST into exactly what you’re talking about, the early universe and its expansion, explaining the beginning, gravity, galaxy and star formation, will be pretty amazing. I wonder if it might lead to new physics beyond Quantum Theory and General Relativity? I hope they point it at Tabbys star IMMEDIATELY. :slight_smile:

Kepler had an amazing run and rather than focus JWST on planets, we just need another Kepler-like spacecraft to continue the work. Who needs a telescope when binoculars will do (poor analogy, but…)? There is enough science just in our own backyard (3000 ly) to keep ten Keplers busy. We’re not done with Big Bang Theory, just yet, but the doors that might open into the REAL universe of universes will certainly seed the next few generations with new science, perhaps new physics, and beyond.

1 Like