Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
Big Idea 3.10 Part 2- Inserting & Deleting Elements From Lists
Learn more about adding and removing elements from lists!
Inserting Elements
The insert
method is intended to add an element to the list at a specified index. The first argument should be an integer (the index), and the second argument should be the value you want to insert.
# Python
alist = ['apple', 'banana', 'cherry']
alist.insert(index, value)
alist.insert('1', 'kiwi') # alist is now ['apple', 'kiwi', 'banana', 'cherry']
The indexOf
method searches the array for the specified element, in this case, ‘apple’. The splice
method is used to insert elements into the array.
%%js
// Javascript
let alist = ['apple', 'banana', 'cherry'];
// Find the index of 'apple'
const index = alist.indexOf('apple'); // index will be 0 because 'apple' is at the first position of the array.
// Insert 'a' before 'apple'
if (index !== -1) {
alist.splice(index, 0, 'kiwi'); // 0 indicates no elements to remove
}
console.log(alist); // Output: ['kiwi', 'apple', 'banana', 'cherry']
Removing Elements
The remove
method is used to remove the first occurrence of a specified value from a list. In this case, you’re trying to remove 'kiwi'
.
# Python
aList.remove('kiwi') # Removes 'kiwi' from the list
print(aList) # Output: ['apple', 'banana', 'cherry',]
Once again use the indexOf
method to find the index of 'kiwi'
. If ‘kiwi’ is found, the splice
method is used. This method modifies the list. The first argument (index) is the starting index (where to begin removing items), and the second argument (1) is the number of items to remove. Here, it removes one item starting from index 3, effectively removing ‘kiwi’.
%%js
// Javascript
let aList = ['apple', 'banana', 'cherry', 'kiwi', 'grape']; // initial list
let index = aList.indexOf('kiwi'); // Find the index of 'kiwi'
if (index !== -1) {
aList.splice(index, 1); // Removes 'kiwi' from the list
}
console.log(aList); // Output: ['apple', 'banana', 'cherry', 'grape']
Deleting an element
This line uses the del
statement to remove the item at index 2
from the list.
# Python
aList = ['apple', 'banana', 'cherry', 'grape'] # initial list
del aList[2] # Deletes 'cherry' at index 2
print(aList) # Output: ['apple', 'banana', 'grape']
This line uses the splice
method to remove an element from the array. The splice method takes two arguments: The first argument (2)
specifies the starting index from which to begin removing elements. Here, it points to the element at index 2
, which is 'cherry'
.
The second argument (1)
indicates how many elements to remove starting from that index. In this case, it will remove 1
element.
%%js
// Javascript
let aList = ['apple', 'banana', 'cherry', 'grape'];
// Delete the element at index 2
aList.splice(2, 1); // Removes 1 element at index 2
console.log(aList); // Output: ['apple', 'banana', 'grape']
Popcorn Hack
- Try adding an element to your list
- Try deleting a different element from your list