List Head Function

PythonPythonBeginner
Practice Now

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

Introduction

In Python, a list is an ordered collection of elements. Each element can be accessed by its index - a non-negative integer that represents the element's position in the list. The first element in the list has index 0, the second has index 1, and so on.

In this challenge, you will write a function that returns the first element of a list, also known as the "head" of the list.

List Head Function

Write a Python function called head(lst) that takes a list as its argument and returns the first element of the list.

Your function should have the following requirements:

  • The function should be named head
  • The function should take a single argument, which is a list
  • The function should return the first element of the list
  • The function should not modify the original list
def head(lst):
  return lst[0]
head([1, 2, 3]) ## 1

Summary

In this challenge, you learned how to extract the first element of a list in Python. You wrote a function called head that takes a list as its input and returns the first element of the list. Remember that the first element of a list always has an index of 0.