Challenge - 5 Problems
List Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this list append and remove code?
Consider the following Python code that adds and removes elements from a list. What will be printed?
Python
fruits = ['apple', 'banana', 'cherry'] fruits.append('date') fruits.remove('banana') print(fruits)
Attempts:
2 left
๐ก Hint
Remember that append adds to the end and remove deletes the first matching element.
โ Incorrect
The list starts with three fruits. 'date' is added at the end with append. Then 'banana' is removed. The final list has 'apple', 'cherry', and 'date'.
โ Predict Output
intermediate2:00remaining
What is the output after popping elements from a list?
Look at this code that pops elements from a list. What will be printed?
Python
numbers = [10, 20, 30, 40] popped = numbers.pop() print(popped, numbers)
Attempts:
2 left
๐ก Hint
pop() removes and returns the last item by default.
โ Incorrect
pop() removes the last element, which is 40, and returns it. The list then has the first three elements left.
โ Predict Output
advanced2:00remaining
What is the output after inserting and removing by index?
What will this code print after inserting and removing elements by index?
Python
letters = ['a', 'b', 'd'] letters.insert(2, 'c') removed = letters.pop(1) print(letters, removed)
Attempts:
2 left
๐ก Hint
insert adds at the given index, pop removes and returns the element at the index.
โ Incorrect
Insert adds 'c' at index 2, so list becomes ['a', 'b', 'c', 'd']. Then pop(1) removes 'b' at index 1. Final list is ['a', 'c', 'd'], removed is 'b'.
โ Predict Output
advanced2:00remaining
What error does this code raise when removing a non-existent element?
What error will this code raise?
Python
colors = ['red', 'green', 'blue'] colors.remove('yellow')
Attempts:
2 left
๐ก Hint
remove() raises an error if the item is not found in the list.
โ Incorrect
Trying to remove 'yellow' which is not in the list raises a ValueError.
๐ง Conceptual
expert2:00remaining
How many elements are in the list after these operations?
Given this code, how many elements remain in the list after all operations?
Python
items = [1, 2, 3, 4, 5] items.append(6) items.pop(0) items.remove(4) items.insert(2, 7)
Attempts:
2 left
๐ก Hint
Count carefully each add and remove operation.
โ Incorrect
Start with 5 elements. Append adds one (6 elements). pop(0) removes one (5 elements). remove(4) removes one (4 elements). insert adds one (5 elements). Final count is 5.