Extracting List Tail in Python

Beginner

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

Introduction

In Python, a list is a collection of items that are ordered and changeable. Sometimes, we need to get all the elements of a list except for the first one. This can be done using the list tail method. In this challenge, you will be asked to implement a function that returns all elements in a list except for the first one.

List Tail

Write a function tail(lst) that takes a list as an argument and returns all elements in the list except for the first one. If the list contains only one element, return the whole list.

def tail(lst):
  return lst[1:] if len(lst) > 1 else lst
tail([1, 2, 3]) ## [2, 3]
tail([1]) ## [1]

Summary

In this challenge, you have learned how to implement a function that returns all elements in a list except for the first one. You have also learned how to handle the case where the list contains only one element. Keep practicing to improve your Python skills!