1/5
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
How do you change a single item in a list?
Refer to the index number.
Code:
fruits = ["apple", "banana", "cherry"]
fruits[1] = "kiwi"
Output: ["apple", "kiwi", "cherry"]
How do you change a range of item values?
Use slice indexing to replace multiple items.
Code:
fruits = ["apple", "banana", "cherry", "orange"]
fruits[1:3] = ["kiwi", "mango"]
Output: ["apple", "kiwi", "mango", "orange"]
What happens when you replace a range with more items than you removed?
The list expands to fit the new items.
Code:
fruits = ["apple", "banana", "cherry"]
fruits[1:2] = ["kiwi", "mango"]
Output: ["apple", "kiwi", "mango", "cherry"]
What happens when you replace a range with fewer items than you removed?
The list shrinks as items are replaced by a smaller set.
Code:
fruits = ["apple", "banana", "cherry"]
fruits[1:3] = ["watermelon"]
Output: ["apple", "watermelon"]
How do you use the insert() method to add an item at a specific index?
insert() adds an item without deleting anything.
Code:
fruits = ["apple", "banana"]
fruits.insert(1, "orange")
Output: ["apple", "orange", "banana"]
Code Challenge: What is the result of replacing [0:2] with ["X"] in the list ["a", "b", "c"]?
Code:
vals = ["a", "b", "c"]
vals[0:2] = ["X"]
Output: ["X", "c"]