Drop List Elements from the Right | Challenge

PythonPythonBeginner
Practice Now

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

Introduction

In Python, we can easily remove elements from a list using slice notation. This can be useful when we want to remove a specific number of elements from the right end of a list. In this challenge, you will create a function that takes a list and an optional number of elements to remove from the right end of the list.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") subgraph Lab Skills python/comments -.-> lab-13092{{"`Drop List Elements from the Right | Challenge`"}} python/lists -.-> lab-13092{{"`Drop List Elements from the Right | Challenge`"}} python/tuples -.-> lab-13092{{"`Drop List Elements from the Right | Challenge`"}} python/function_definition -.-> lab-13092{{"`Drop List Elements from the Right | Challenge`"}} python/default_arguments -.-> lab-13092{{"`Drop List Elements from the Right | Challenge`"}} end

Drop List Elements from the Right

Problem

Write a function drop_right(a, n = 1) that takes a list a and an optional integer n and returns a new list with n elements removed from the right end of the list a. If n is not provided, the function should remove only the last element from the list.

Example

drop_right([1, 2, 3]) ## [1, 2]
drop_right([1, 2, 3], 2) ## [1]
drop_right([1, 2, 3], 42) ## []

Summary

In this challenge, you learned how to remove elements from the right end of a list using slice notation in Python. You also created a function that takes a list and an optional integer and returns a new list with n elements removed from the right end of the list.

Other Python Tutorials you may like