0
0
Pythonprogramming~20 mins

Adding and removing list elements in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
List Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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)
A['apple', 'cherry']
B['apple', 'banana', 'cherry']
C['apple', 'cherry', 'date']
D['apple', 'banana', 'cherry', 'date']
Attempts:
2 left
๐Ÿ’ก Hint
Remember that append adds to the end and remove deletes the first matching element.
โ“ Predict Output
intermediate
2: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)
A10 [20, 30, 40]
B30 [10, 20, 40]
C40 [10, 20, 30, 40]
D40 [10, 20, 30]
Attempts:
2 left
๐Ÿ’ก Hint
pop() removes and returns the last item by default.
โ“ Predict Output
advanced
2: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)
A['a', 'c', 'd'] b
B['a', 'b', 'c', 'd'] b
C['a', 'b', 'd'] c
D['a', 'c', 'd'] c
Attempts:
2 left
๐Ÿ’ก Hint
insert adds at the given index, pop removes and returns the element at the index.
โ“ Predict Output
advanced
2: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')
AValueError
BKeyError
CIndexError
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
remove() raises an error if the item is not found in the list.
๐Ÿง  Conceptual
expert
2: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)
A7
B5
C4
D6
Attempts:
2 left
๐Ÿ’ก Hint
Count carefully each add and remove operation.