Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.10.2 Accessing and Deleting Elements
Accessing an Element
1. Positive Indexing
- Definition: Accessing elements using their position from the start of the list.
- Indexing Starts: From
0
. - Example:
- For a list:
my_list = [10, 20, 30, 40, 50]
- Accessing elements:
my_list[0]
→ 10 (first element)my_list[1]
→ 20 (second element)my_list[2]
→ 30 (third element)
- For a list:
2. Negative Indexing
- Definition: Accessing elements using their position from the end of the list.
- Indexing Starts: From
-1
for the last element. - Example:
- For the same list:
my_list = [10, 20, 30, 40, 50]
- Accessing elements:
my_list[-1]
→ 50 (last element)my_list[-2]
→ 40 (second to last element)my_list[-3]
→ 30 (third to last element)
- For the same list:
Summary
- Use positive indexing (
0, 1, 2, ...
) to access elements from the start. - Use negative indexing (
-1, -2, -3, ...
) to access elements from the end.
This allows you to easily access any element in a list!
(Javascript):
%%js
let aList = [];
while (true) {
let user_input = prompt("Enter an item you want (or 'q' to quit):");
if (user_input.toLowerCase() === 'q') {
break;
}
aList.push(user_input);
}
// Access and display the second element (index 1)
console.log("The second thing on your list is: " + aList[1]);
(Python):
aList = []
while True:
user_input = input("Enter an item you want (or 'q' to quit): ")
if user_input.lower() == 'q':
break
aList.append(user_input)
//Displaying the second element (index 1)
if len(aList) > 1:
print("The second thing on your list is", aList[1])
else:
print("There is no second item in your list.")
//Display a range of data from index 1 to 3 (non-inclusive)
print("Here is a range of data (index 1 to 3):", aList[1:3])
Deleting an Element
-
Using
remove(value)
: Removes the first occurrence of a specified value from the list(Python). -
Using
pop(index)
: Removes and returns the element at the given index. If no index is provided, it removes the last element(Python & Javascript). -
Using
del
: Deletes an element at a specified index or can remove the entire list(Python). -
Using
splice(start, deleteCount)
(JavaScript): Modifies the array by removing elements starting at a specified index(Javascript).
These methods allow you to effectively delete elements from lists or arrays in programming!
Examples(JavaSript):
%%js
let aList = [];
while (true) {
let user_input = prompt("Enter an item you want (or 'q' to quit):");
if (user_input.toLowerCase() === 'q') {
break;
}
aList.push(user_input);
}
// Display the full list
console.log("This is your list: ", aList);
// Delete the second element (index 1)
aList.splice(1, 1);
// Display the list after deletion
console.log("This is your new list: ", aList);
(Python):
aList = []
while True:
user_input = input("Enter an item you want (or 'q' to quit): ")
if user_input.lower() == 'q':
break
aList.append(user_input)
print("This is your list:", aList)
# Deleting the second item (index 1)
if len(aList) > 1:
del aList[1]
print("This is your new list after deleting the second item:", aList)
else:
print("There is no second item to delete.")
Explanation:
Accessing an Element:
The code first asks the user to input items into the list/array. It then checks if there are at least two elements and prints the second one (index 1). If not, it displays a message stating there is no second item.
Deleting an Element:
The code removes the second element from the list/array using del
in Python and splice()
in JavaScript, and then prints the updated list/array. If there is no second item, a message is displayed instead.
Popcorn Hack: Accessing and Deleting Elements
- Create a list/array named aList.
- Input items into the list/array. After the user is done adding items, display the second element (if it exists) in the list/array.
- After adding items to the list/array, delete the second element (if it exists) and display the updated list/array.