Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Merge first and last elements separately in a list
Next article icon

Get first and last elements of a list in Python

Last Updated : 21 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of getting the first and last elements of a list in Python involves retrieving the initial and final values from a given list. For example, given a list [1, 5, 6, 7, 4], the first element is 1 and the last element is 4, resulting in [1, 4].

Using indexing

This is the most straightforward way to get the first and last elements. We can access the first element with a[0] and the last element with a[-1]. It is quick and easy to understand, making it the most commonly used approach.

Python
a = [1, 5, 6, 7, 4]  res = [a[0], a[-1]] print(res) 

Output
[1, 4] 

Explanation:[a[0], a[-1]] creates a new list res containing the first element of a (accessed by a[0]) and the last element of a (accessed by a[-1]).

Table of Content

  • Using unpacking
  • Using list comprehension
  • Using operator.itemgetter

Using unpacking

Unpacking allows us to extract the first and last elements while ignoring the rest. Using first, *_, last = a gives the first and last elements directly. It is cleaner when we only care about the ends and don’t need the elements in between.

Python
a = [1, 5, 6, 7, 4]  first, *_, last = a  res = [first, last] print(res) 

Output
[1, 4] 

Explanation: first, *_, last = a assigns the first element (1) to first, the last element (4) to last, and the middle elements [5, 6, 7] to _, which is ignored as _ is a placeholder for unused values. Then, res = [first, last] creates a new list [1, 4].

Using list comprehension

List comprehension can be used to pick specific positions from a list. You can get the first and last elements using [a[i] for i in (0, -1)]. This method works well but is less readable compared to indexing.

Python
a = [1, 5, 6, 7, 4]  res = [a[i] for i in (0, -1)] print(res) 

Output
[1, 4] 

Explanation:[a[i] for i in (0, -1)] create a new list by accessing a[0] (first element 1) and a[-1] (last element 4), resulting in [1, 4], which is stored in res.

Using operator.itemgetter

itemgetter() function from the operator module fetches elements by index. We can get the first and last elements using itemgetter(0, -1)(a). It is useful when we need multiple elements by position, but less common for just two values.

Python
from operator import itemgetter  a = [1, 5, 6, 7, 4]  getter = itemgetter(0, -1) res = list(getter(a)) print(res) 

Output
[1, 4] 

Explanation: itemgetter(0, -1) creates a getter function to fetch the first (a[0] → 1) and last (a[-1] → 4) elements from a. getter(a) extracts these values and list() converts them into [1, 4], stored in res.



Next Article
Python | Merge first and last elements separately in a list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Get Last N Elements from Given List
    This article discusses ways to fetch the last N elements of a list in Python. Example: Using list Slicing [GFGTABS] Python test_list = [4, 5, 2, 6, 7, 8, 10] # initializing N N = 5 # using list slicing res = test_list[-N:] print(str(res)) [/GFGTABS]Output[2, 6, 7, 8, 10] Explaination: This problem c
    3 min read
  • Get the Last Element of List in Python
    In this article, we will learn about different ways of getting the last element of a list in Python. For example, consider a list: Input: list = [1, 3, 34, 12, 6]Output: 6 Explanation: Last element of the list l in the above example is 6. Let's explore various methods of doing it in Python: 1. Using
    2 min read
  • Python | Merge first and last elements separately in a list
    Given a list of lists, where each sublist consists of only two elements, write a Python program to merge the first and last element of each sublist separately and finally, output a list of two sub-lists, one containing all first elements and other containing all last elements. Examples: Input : [['x
    2 min read
  • Python program to interchange first and last elements in a list
    Given a list, write a Python program to swap the first and last element of the list using Python. Examples: The last element of the list can be referred to as a list[-1]. Therefore, we can simply swap list[0] with list[-1]. [GFGTABS] Python # Initialize a list my_list = [1, 2, 3, 4, 5] # Interchange
    5 min read
  • Get first element from a List of tuples - Python
    The goal here is to extract the first element from each tuple in a list of tuples. For example, given a list [(1, 'sravan'), (2, 'ojaswi'), (3, 'bobby')], we want to retrieve [1, 2, 3], which represents the first element of each tuple. There are several ways to achieve this, each varying in terms of
    2 min read
  • Python | Indices of N largest elements in list
    Sometimes, while working with Python lists, we can have a problem in which we wish to find N largest elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Let's discuss a certain way to fi
    5 min read
  • Shift Last Element to First Position in list - Python
    The task of shifting the last element to the first position in a list in Python involves modifying the order of elements such that the last item of the list is moved to the beginning while the rest of the list remains intact. For example, given a list a = [1, 4, 5, 6, 7, 8, 9, 12], the goal is to sh
    3 min read
  • Python | Remove given element from list of lists
    The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
    6 min read
  • Append Elements to Empty List in Python
    In Python, lists are used to store multiple items in one variable. If we have an empty list and we want to add elements to it, we can do that in a few simple ways. The simplest way to add an item in empty list is by using the append() method. This method adds a single element to the end of the list.
    2 min read
  • Remove last K elements of list - Python
    Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Let’s explore different methods to remove the last K elements from a list in Python. Using list slicingList slicing is one of
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences