Exercises
Remove Items
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Remove Grace by name.
Python
names = ["Ada", "Grace", "Katherine"]
# remove Grace
print(names)remove takes the value, not the position.
names = ["Ada", "Grace", "Katherine"]
names.remove("Grace")
print(names)Exercise 2Passed
Take the last item off and print it, leaving the rest in the list.
Python
names = ["Ada", "Grace", "Katherine"]
# print the removed item
print(names)pop() with no argument takes the last one and hands it back.
names = ["Ada", "Grace", "Katherine"]
print(names.pop())
print(names)Exercise 3Passed
Keep only the odd numbers, by building a new list rather than removing while looping.
Python
numbers = [1, 2, 3, 4, 5, 6]
# keep the odd ones
print(numbers)A comprehension with a filtering if.
numbers = [1, 2, 3, 4, 5, 6]
numbers = [n for n in numbers if n % 2 != 0]
print(numbers)