Drop List Elements from the Left

Beginner

This tutorial is from open-source community. Access the source code

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.

Drop List Elements from the Left

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) ## []

Summary

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.