Python · Lists
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Clear vs Delete List
What's the difference between
items.clear()
and
items = []
?
Options
• clear() empties the existing list, = [] creates a new empty list
• They do exactly the same thing
• clear() is faster than = []
• = [] removes the list entirely
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.
inventory = ["a", "b", "c", "d", "e", "g"] every_third = inventory____________________ print(every_third)
Options: [ : 3 ] 2 : 4
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 to match
• items.count(2)
• items[-2:]
• len(items)
• items.index(4)
Definitions
• 3
• [2, 4]
• 6
• 5
Exercise 4 — Append vs Extend
What's wrong with this code?
codemon_attacks = ["fire", "water"] special_attacks = ["thunder", "ice"] codemon_attacks.append(special_attacks)
Options
• It adds the entire list special_attacks as a single element
• It causes an error
• It only adds the first attack
• It doesn't add anything
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"]
Options
• Returns the shortest string (```"ice"```)
• Returns alphabetically first (```"fireball"```)
• Returns the string with least characters
• Raises an error
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]
?
Options
• ```[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.
attacks = ["fire", "ice", "wind"] removed_attack = attacks.____(____) print(attacks)
Options: pop 1 remove 2
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"))
Options
• Returns the first occurrence (0)
• Returns the last occurrence (2)
• Returns all occurrences [0, 2]
• Raises an error
Hint : Try to remember how the index method works.
Exercise 9 — Insert Between Elements
Insert a new item between the first and second elements.
items = ["first", "third"] items.____(________ "second") print(items)
Options: insert 1 , append 0 2
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?
Options
• ```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 to match
• items + items
• items * 2
• [items, items]
• items[:] + ["d"]
Definitions
• ["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
Options
• ```[1, 2, 3, 1, 2, 3]```
• ```[2, 4, 6]```
• ```[[1, 2, 3], [1, 2, 3]]```
• Error
Hint : Think about what multiplying a list means.
Exercise 13 — Insert at Middle Position
Add an item to the middle of a list.
items = ["start", "end"] middle_index = len(items) ____ ____ items.____(middle_index____ "middle") print(items)
Options: // 2 insert , % 4 append
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))
Options
• ```3```
• ```4```
• ```5```
• Error
Hint : 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.
items = ["old", "keep", "old"] items____________ = "first" items____________ = "last" print(items)
Options: [ 0 ] -1 ( ) ( ) [ ] 3
Hint : Think about positive and negative indexing.
Exercise 16 — Correct List Combination
Which method correctly adds new items from another list?
Options
• ```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.
Items to match
• list.append(x)
• list.extend(x)
• list + [x]
• list.insert(0, x)
Definitions
• Adds x as a single item
• Adds items from x individually
• Creates new list with x added
• Adds x at position 0
Exercise 18 — Middle Elements Slice
Which slice gets all elements except first and last?
items = [1, 2, 3, 4, 5]
Options
• ```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()
Options
• IndexError: pop from empty list
• Returns None
• Nothing happens
• Returns an empty list
Hint : Think about list requirements.
Exercise 20 — Reversed List Copy
Create a reversed copy of a list without modifying the original.
original = [1, 2, 3, 4] reversed_copy = original____________________ print(reversed_copy)
Options: [ : : -1 ] -2 0
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]
Options
• Returns True if all elements are in the same order, and match
• Always returns False
• Compares only the first element
• Raises a TypeError
Hint : Think about list comparison rules.
Exercise 22 — List Slicing Operations
Match each list operation with its behavior.
Items to match
• items[:]
• items[::-1]
• items[::2]
• items[-3:]
Definitions
• Creates a copy
• Reverses the list
• Every second item
• Last three items
Exercise 23 — Repeated Values List
Create a list containg "codeball" 5 times.
value = "codeball" repeated = ____value________ ____ print(repeated)
Options: [ ] * 5 ( ) +
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
Options
• ```[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.
team = ["Aquachu", "Pyrochu", "Florachu", "Zap"] second_codemon = team____________ print(second_codemon) last_codemon = team____________ print(last_codemon)
Options: [ 1 ] -1 0 4 2 [ ]
Hint : Remember that list indexing starts at 0, and negative indices count from the end.
Back to the course