Remove Elements from a slice in Go

Jeff Roberts
InstructorJeff Roberts
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

Go has a special indexing syntax that forms the basis of removing one or more elements from a slice. I will teach you this syntax and show you its various forms.

Based upon that, I'll show you how to remove a single element from a slice. However, you may find this somewhat limiting. I also will show you a more typical example of removing elements from a slice as we iterate over it. This technique shows you how to remove multiple elements from a slice.

Instructor: [00:00] How do we remove from a slice? Well, let's start with some champions that I've loaded from a file. Let's take a look at what this looks like. This is just a JSON file with a bunch of champions in it from a game I like to play called Teamfight Tactics. Each champion looks like this.

[00:19] If I look at the data.go file, each champion has its a strap that has a name, it has some classes, some origins and a gold cost. Let's go ahead and run this and we'll see that I've loaded up a whole bunch of champions. The slice says 56 elements and you can see all of the champions that I've loaded from the file.

[00:46] Removing elements from a slice uses a syntax that's similar to the indexing syntax that you're probably used to when you work with arrays or slices. I've loaded all these champions into a variable called all, which is a slice. I'm going to use here the index syntax to grab the very first champion out of this slice, and in the next line, at line 17, I'm going to print that to the console.

[01:17] Let's ran this and we can see when I expand this, the first champion is Akali. It's actually the first element in the slice. In Go, I can use a different type of indexing to grab a chunk of the slice by specifying using this syntax the from index and the to index.

[01:43] Now, when I specify this, the from index is inclusive. Meaning, it will include the element as that element in the first index, but it will exclude the last. It will include everything up to element 20, but not including element 20. Then I'm going to print this out. Let's see what happens here.

[02:06] If we take a look, it says the first 20 champions are and it lists champions from 0through 19, since slices are zero-based. Go has some shortcuts. I'm just going to overwrite this same variable that I already declared just so that I don't have to comment anything out. I can leave out the first element, the first index and it will assume zero.

[02:35] If I ran this again, we can see that it has the same effect of specifying both. It still grab the first 20 champions and it's the same list that I had when I specified both indexes. These two lines are actually equivalent. Guess what, if I can omit the first, then I can omit the last.

[03:00] I'm going to re-declare this or reassign the same variable and I'm going to specify 10 for the first index and leave out the last index. What that means is that it will take everything up through the end of the slice.

[03:15] Let's ran this and it should take 10 though the last element in the slice. This time, it takes 46 champions. You guessed it, if I can omit the first and I can omit the last, then I can omit them all. I'm not going to specify any indexes here. The first one defaults to zero and the last one defaults to the end.

[03:42] When I ran this, I'm going to get the entire array, 56 champions which are the same number that I loaded from the file. Why did I show you this? Because removing elements from a slice is based upon this syntax that I just showed you right up here.

[04:03] Here, I'm going to show you how to remove one element from a slice. I have a new variable called most and I'm using the built in append function and I'm using this indexing syntax to take elements zero through nine.

[04:22] Remember, the last element is excluded and append takes the slice that I want to add to or append to and then a list of one or more elements that I'm going to add to it. I'm going to use the same syntax to take elements 11 through the end.

[04:46] I'm using the three dots syntax which tells the compiler to enumerate all of those as if I've typed them all out. Then I'm going to log out. How many champions are in the most variable and then how many champions are in the all variable which should be unaltered.

[05:05] Let's ran this and we can see when I expand this, if I scroll back a little, there are 55 champions in the most variable. I omitted the champion at element 10, which should be Fiora which is right here, right after Draven. If I look down here and I find Draven right here, there is no Fiora.

[05:40] Fiora is gone because I removed just that one element. You can see the all slice is unaltered, it still has 56 elements. I used this syntax that I taught you above to extract or remove one element from the slice and append to create a new slice which I assigned to the variable most.

[06:07] This technique that I taught you up here is not that commonly used because it requires you to know the specific index or continuous indexes of the elements that you want to remove. What I'm going to show you now is a more common usage where you might be iterating over a slice and you want to dynamically determine if you want to remove something.

[06:36] This little block of code here is going to iterate using range over all the champions and I'm ignoring the index variable, but I care about the champ. I initialized this index counter before the loop to zero and I'm going to omit all sorcerers from the all slice.

[07:01] If the champion that I'm currently iterating over has a class of sorcerer, then I'm just going to continue which skips the rest of this loop. If it's not a sorcerer, then I'm going to change the all slice in place by setting at the index zero in the first case. I'm initializing or setting that to the champ that I'm on and then I'm incrementing the index.

[07:33] If you look at this code closely, it might be a little confusing, I am altering the all slice in place by keeping a separate counter and omitting sorcerers. Outside of the loop, I'm using the syntax that I taught you to create a new slice from zero up to and excluding the last index.

[07:59] I'll print the results of that out and what we will see here is that I have 47 champions. If you look closely at this, none of them are sorcerers because they got removed dynamically using this little bit of code that I wrote right here.