Remove the First and Last Element from a Python List
In this Python programming exercise, we are going to see how we can remove the first and the last elements from the list.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now what I want to see happen with this is I'm going to type out some comments so we can see exactly how this should work. So I want to build out a function and we'll call it remove_first_and_last you can call yours whatever you'd like. And then it's also going to have a list to clean past into it. And so inside of here or I'm not going to build it out yet, that's all in the solution. But what I want to have happen is when you pass in some kind of a list so say that I have some HTML elements and so I'll have some strings inside of here one could be an h1 and then after that would be some content followed by a closing h1 tag and that would be a list.

Let me shrink this just to make it easier to see on one line. So this would be an example of the list. If I were to call remove first and last pass in the HTML the output that I would be looking for here would be simply some content. So I just would want to have some content and that's all I would want returned because I want to have this first element and this last element removed.

large

Now that by itself is not that hard because you could essentially just build something that went and grabbed the second element in the array, but let's imagine that we extend this a little bit. So if I come down here and now we have we'll call it html_2 and so we have html_2. Now we have some content but we also have more content here. What I would want if I called html_2 is someone content and then more and just to make it very clear what should get returned here is a list. So let me just do that just so it's not confusing. You're not returning a string the goal of this should be to return some type of list and that should be what gets returned whether it's one item, two items, or a thousand items.

So if someone were to pass a thousand and a few items into your function then it should have the exact same behavior where it drops off the first and the last element. And if you're curious on how this could be beneficial this is something that I have to build out quite a bit or at least some variation of this whenever I'm performing any kind of task such as building out a web scraper or anything like that any time where I know beyond a shadow of a doubt that I'm going to be getting at least 3 elements back or 3 elements are going to be inside of a list and the first and the last elements are just kind of throwaway elements. Then this is something that can be quite helpful so I recommend if you're going through this that you pause the video right now, and you try to build this out on your own and then come back and watch my own solution to it.

large

If you did that great job and now I'm going to walk you through what my own solution for this would be. I'm gonna copy this line because this is exactly the start of the definition that I want to use. So I'm gonna say def remove first and last.

def remove_first_and_last(list_to_clean):

Now inside of here there really is not a lot of work we have to do in order to make this possible. If you follow along and you follow my javascript or my Ruby programming exercises then what you may have seen because I've built this out in those languages as well is that there were a few other kind of syntax options in those languages but I really like the way that python does this and in fact Python has probably the best way of doing this because you have the ability to glob the elements in the list.

If you do not know what globbing is, what it allows you to do, I'll come down here and write it in a comment. So imagine that you have some kind of a list, you could have 1, 2, and 3 in the list you have the ability because you can perform tasks such as destructuring. You have the ability to grab these elements so you could say 1, 2, and 3 and then what you would get is the integer 1 would be stored in the one variable, integer two in the 2, and the three would be in the 3.

But now if you had more than these elements so if you had 3 4 and 5 what you can do is right here in this second variable you can put an asterix right in front of it and what's going to happen is it is going to glob everything in the middle. So one is still going to grab the very first element in the list and then 3 is going to grab the very last element. In this case, it is 5 and then this is what we have here in the center, this 2 variable is going to actually be a list.

We can test this out in the REPL just in case you don't believe me. Let's say python and I'm going to do exactly what we just did. So say one, two, and then three places in our variables here and now one is equal to 1, two is equal to 2, and three is equal to 3. Now if we want to extend this a little bit we can say 1 and then put our glob of 2 and then 3 and then we can put any number of items just like this.

one, *two, three = [1, 2, 3, 45, 6]

So one still equal to 1 three should be equal to 6 and then look at two, two is equal to a list inside, two is everything inside. So because of that, we can build our solution with this code.

large

So I'm going to clear the terminal and switch back to the code and we can keep this up here in the comments just you can have them for the show notes. And now I'm going to use destructuring. Now a common convention inside Python whenever you're assigning a variable that you are not going to use, you assign it to the underscore.

So I'm not going to use the first or the last elements so I'm going to say underscore comma and then all do Asterix content comma followed by another underscore and then simply pass in list_to_clean and then I'm going to return content and notice when I am returning content I'm not putting Asterix there. That is only necessary when you are performing the destructuring and your globbing that.

So now that we have that let's test this out with some of our test cases up here so I'm going to grab this HTML list and the remove statement and let's also print it out. So print first and last save this and now let's run it. So python this file is called I believe remove first and last. So Python remove_first_and_last.py and that worked. You can see that we have some content right there which is exactly what we're looking for. So this successfully removed the first and last.

large

Now we can also test it out with our second test case right here. If I come down and get rid of our first one and now let's just print this out. If I run it again you can see it returned 'Some content' and then 'more' so it is globbing up all of the elements inside the list besides the first one and the last one which get dropped.

large

So out of all the languages that I've built out this specific code exercise for, Python really I think has one of the cleanest syntaxes for it because of destructuring and because you're able to glob up these elements it's a really nice and easy way to read the way that this works. And also within one line, you can see how quickly you're able to grab those elements and then you can use that content however you want.

Now if you're looking for a way to have even a little bit more of a challenge I would say you could do is implement a conditional inside of this function that checks to make sure that a list here whatever gets passed in contains at least three or more elements. Imagine a scenario where you try to perform this type of structuring and you had one element or you didn't have anything than what you should do is you should raise some type of error.

You should say that you need to put in at least three different elements inside of whatever list you're passing in or else you get an error. If you're building out some type of python module for that had this kind of behavior in it, it would be very helpful to give some good error messages whenever someone's using it improperly. So if you're looking for a way to extend this exercise and learn a little bit more about it then I think that would be a nice way of adding to it.

Resources