Introduction
In Python, we can use slice notation to remove elements from a list. In this challenge, you will need to write a function that removes a specified number of elements from the left of a list.
This tutorial is from open-source community. Access the source code
In Python, we can use slice notation to remove elements from a list. In this challenge, you will need to write a function that removes a specified number of elements from the left of a list.
Write a function drop(a, n=1)
that takes a list a
and an optional integer n
as arguments and returns a new list with n
elements removed from the left of the original list. If n
is not provided, the function should remove only the first element of the list.
def drop(a, n = 1):
return a[n:]
drop([1, 2, 3]) ## [2, 3]
drop([1, 2, 3], 2) ## [3]
drop([1, 2, 3], 42) ## []
In this challenge, you learned how to use slice notation to remove elements from a list in Python. You also wrote a function that removes a specified number of elements from the left of a list.