Change List Items

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/5

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

6 Terms

1
New cards

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"]

2
New cards

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"]

3
New cards

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"]

4
New cards

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"]

5
New cards

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"]

6
New cards

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"]