Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Clear vs Delete List
What's the difference between items.clear() and items = []?
Hint : Think about what happens to references when you modify vs reassign a list.
Exercise 2 — Codémon Inventory Selection
Get every third item from the Codémon inventory, starting from the beginning.
Hint : Slicing is done with the syntax list[start:end:step]
Exercise 3 — List Method Results
Match each operation with its result for items = [1, 2, 2, 3, 2, 4].
items.count(2)items[-2:]len(items)items.index(4)3[2, 4]65Exercise 4 — Append vs Extend
What's wrong with this code?
codemon_attacks = ["fire", "water"]
special_attacks = ["thunder", "ice"]
codemon_attacks.append(special_attacks)Hint : Think about the difference between append and extend. One add one element, the other adds multiple elements.
Exercise 5 — String Comparison in Lists
What happens when you use min() on a list of strings?
codemon_attacks = ["fireball", "ice", "thunder"]
"ice")"fireball")Hint : min() with numbers returns the smallest number, but with strings, what would be more logical ? Think about how strings are compared.
Exercise 6 — Negative Step Slicing
What will items[::-2] return for items = [1, 2, 3, 4, 5]?
[5, 3, 1][1, 3, 5][2, 4][4, 2]Hint : Think about negative step and its effect.
Exercise 7 — Remove Codémon Attack
Remove the "ice" attack from the list.
Hint : Think about which method removes and returns an item.
Exercise 8 — Index Method Behavior
What happens when you try to find the index of an item that appears multiple times?
items = ["a", "b", "a", "c"]
print(items.index("a"))Hint : Try to remember how the index method works.
Exercise 9 — Insert Between Elements
Insert a new item between the first and second elements.
Hint : append() adds an element to the end, the other function adds where you want but you need to precise the index!
Exercise 10 — Empty List Check
What's the most efficient way to check if a list is empty?
if not items:if len(items) == 0:if items == []:if items.count() == 0:Hint : We're talking about an elegant solution, something short, easy to read.
Exercise 11 — List Combination Methods
Match the result for items = ["a", "b"].
items + itemsitems * 2[items, items]items[:] + ["d"]["a", "b", "a", "b"]["a", "b", "a", "b"][["a", "b"], ["a", "b"]]["a", "b", "d"]Exercise 12 — List Multiplication Result
What's the result of this operation?
[1, 2, 3] * 2
[1, 2, 3, 1, 2, 3][2, 4, 6][[1, 2, 3], [1, 2, 3]]Hint : Think about what multiplying a list means.
Exercise 13 — Insert at Middle Position
Add an item to the middle of a list.
Hint : Think about how to find the middle index.
Exercise 14 — Nested List Length
What will this code produce?
items = [1, [2, 3], 4]
print(len(items))345Hint : Try to count the elements, not the elements and their contents.
Exercise 15 — Replace First and Last
Replace the first and last items with new values.
Hint : Think about positive and negative indexing.
Exercise 16 — Correct List Combination
Which method correctly adds new items from another list?
list1.extend(list2)list1.append(list2)list1.add(list2)list1.insert(list2)Hint : Think about combining lists.
Exercise 17 — List Method Behaviors
Match the operation with its behavior.
list.append(x)list.extend(x)list + [x]list.insert(0, x)Exercise 18 — Middle Elements Slice
Which slice gets all elements except first and last?
items = [1, 2, 3, 4, 5]
items[1:-1]items[1:5]items[0:-1]items[:-1]Hint : Think about slice boundaries. We include the start index and exclude the end index.
Exercise 19 — Empty List Pop Error
What happens if you try to remove an item from an empty list?
empty = []
empty.pop()Hint : Think about list requirements.
Exercise 20 — Reversed List Copy
Create a reversed copy of a list without modifying the original.
Hint : Think about step in slicing and negative indexing.
Exercise 21 — List Comparison Rules
When comparing two lists, what happens?
[1, 2, 3] == [1, 2, 3]
Hint : Think about list comparison rules.
Exercise 22 — List Slicing Operations
Match each list operation with its behavior.
items[:]items[::-1]items[::2]items[-3:]Exercise 23 — Repeated Values List
Create a list containg "codeball" 5 times.
Hint : Think about list multiplication.
Exercise 24 — List Reference Behavior
What will be the final value of scores?
scores = [10, 20, 30]
new_scores = scores
scores[0] = 99[99, 20, 30][10, 20, 30][10, 20, 30, 99][99]Hint : Think about list index. They always starts at .. 0.
Exercise 25 — Access Codémon Team Members
Access the second and last Codémon from your team.
Hint : Remember that list indexing starts at 0, and negative indices count from the end.