List Head Function | Challenge

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.


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`") subgraph Lab Skills python/comments -.-> lab-13121{{"`List Head Function | Challenge`"}} python/lists -.-> lab-13121{{"`List Head Function | Challenge`"}} python/tuples -.-> lab-13121{{"`List Head Function | Challenge`"}} python/function_definition -.-> lab-13121{{"`List Head Function | Challenge`"}} end

List Head Function

Problem

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

Example

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.

Other Python Tutorials you may like