---
title: "How to get the first and last element from the List"  
description: "How to get the first and last element from the List"  
author: "Revati S Misra"  
published: 2023-08-21  
updated: 2023-08-23  
canonical: https://www.mindstick.com/forum/159622/how-to-get-the-first-and-last-element-from-the-list  
category: "data structure"  
tags: ["array list", "list", "data structure"]  
reading_time: 2 minutes  

---

# How to get the first and last element from the List

How to get the first and last element from the List

## Replies

### Reply by Aryan Kumar

Sure, there are a few ways to get the first and last element from a list in Python.

Here are two ways to get the first and last element from a list:

1. Using the `list.index()` method:

Python

```plaintext
list = [1, 2, 3, 4, 5]

# Get the first element from the list using the list.index() method.
first_element = list.index(0)

# Get the last element from the list using the list.index() method.
last_element = list.index(len(list) - 1)
```

In this example, the `list` variable is a list that has the values 1, 2, 3, 4, and 5. The `list.index()` method returns the index of the element in the list. The `first_element` variable is assigned the value of the first element in the list, which is 1. The `last_element` variable is assigned the value of the last element in the list, which is 5.

1. Using the `list[0]` and `list[-1]` syntax:

Python

```plaintext
list = [1, 2, 3, 4, 5]

# Get the first element from the list using the list[0] syntax.
first_element = list[0]

# Get the last element from the list using the list[-1] syntax.
last_element = list[-1]
```

In this example, the `list` variable is a list that has the values 1, 2, 3, 4, and 5. The `list[0]` syntax returns the first element in the list, which is 1. The `list[-1]` syntax returns the last element in the list, which is 5.

Which method you use depends on your specific needs. If you need to get the first and last element and you don't need to do any further processing on the elements, then the `list.index()` method is a good option. If you need to do further processing on the elements, then the `list[0]` and `list[-1]` syntax is a good option.


---

Original Source: https://www.mindstick.com/forum/159622/how-to-get-the-first-and-last-element-from-the-list

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
