Slicing and List Methods
Taking part of a list
A slice is a copy of part of a list. You ask for one with theslice method, handing it the position to start at and the position to stop before.Read those last two again. Give
slice only a start and there is nothing to stop it, so it runs to the end of the list. Give it nothing at all and it copies the whole thing, which is the everyday way to make a copy of a list before you go changing it.Counting from the right
A negative position counts backwards from the end of the list. Position -1 is the last item, -2 is the one before it, and so on. Sotides.slice(-2) hands back the last two items, [ 6, 38 ].The program below takes a slice of six plant heights, counts it, and takes the last two heights. Run it, then change it and run it again:
• Set
start and end to the same number.• Change
plants.slice(-2) to plants[-2].• Delete
, end from the first slice, so that it reads plants.slice(start).let plants = [69, 85, 56, 97, 75, 59];
let start = 1;
let end = 4;
let middle = plants.slice(start, end);
console.log(middle);
console.log("Heights in the slice:", middle.length);
console.log("Last two:", plants.slice(-2));
console.log("Original:", plants);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
To skip items, step a counter yourself. Whatever number you add to the counter on each pass becomes the size of the gap it leaves: add 2 and it lands on every other position, add 3 and it lands on every third.
The counter lands on index 0 and index 3, then runs past the end and the loop stops. Printing what it finds is only one option: hand the value to
push instead and you are building a new list out of the positions you chose to visit.Reversing a list
reverse turns a list back to front. It does it to the list you called it on, though, so the original does not survive.Four methods that change a list
These four all rearrange the list itself rather than handing back a copy.| Method | What it does |
|---|---|
push(value) | adds value to the END. Hands back the new length. |
pop() | removes the LAST item and hands it back. |
shift() | removes the FIRST item and hands it back. Everything else slides down one position. |
splice(i, n) | removes n items starting at index i, and hands back a list of what it removed. |
splice(i, 0, value) | removes nothing and inserts value AT index i, sliding everything from there rightwards. |
The program below runs a waiting list of ticket numbers through
push, shift, and splice. Run it, then change it and run it again:• Change
tickets.shift() to tickets.pop().• Change
tickets.splice(1, 0, 79) to tickets.splice(3, 0, 79).• Change
tickets.splice(2, 1) to tickets.splice(2, 2).let tickets = [89, 96, 98];
tickets.push(105);
console.log("After push:", tickets);
let served = tickets.shift();
console.log("Served:", served);
tickets.splice(1, 0, 79);
console.log("After the insert:", tickets);
let removed = tickets.splice(2, 1);
console.log("Removed:", removed);
console.log("Now:", tickets);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.