
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Get first and last elements of a list in Python
There may be situation when you need to get the first and last element of the list. The tricky part here is you have to keep track of the length of the list while finding out these elements from the lists. Below are the approaches which we can use to achieve this. But of course all the approaches involve using the index of the elements in the list.
Using only index
In any list the first element is assigned index value 0 and the last element can be considered as a value -1. So we apply these index values to the list directly and get the desired result.
Example
Alist = ['Sun','Mon','Tue','Wed','Thu'] print("The given list : ",Alist) print("The first element of the list : ",Alist[0]) print("The last element of the list : ",Alist[-1])
Output
Running the above code gives us the following result −
The given list : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu'] The first element of the list : Sun The last element of the list : Thu
List slicing
List slicing is another method in which we directly refer to the positions of elements using the slicing technique using colons. The first element is accessed by using blank value before the first colon and the last element is accessed by specifying the len() with -1 as the input.
Example
Alist = ['Sun','Mon','Tue','Wed','Thu'] print("The given list : ",Alist) first_last = Alist[::len(Alist)-1] print(first_last)
Output
Running the above code gives us the following result −
The given list : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu'] ['Sun', 'Thu']
Using For loop
We can also use a for loop with in operator giving the index values as 0 and -1.
Example
Alist = ['Sun','Mon','Tue','Wed','Thu'] print("The given list : ",Alist) first_last = [Alist[n] for n in (0,-1)] print(first_last)
Output
Running the above code gives us the following result −
The given list : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu'] ['Sun', 'Thu']
- Related Articles
- Python program to interchange first and last elements in a list
- Python program to get first and last elements from a tuple
- Python – Find the distance between first and last even elements in a List
- Java Program to Get First and Last Elements from an Array List
- Get last N elements from given list in Python
- Get first and last elements from Vector in Java
- Get first and last elements from Java LinkedList
- How to change first and last elements of a list using jQuery?
- How to get first and last elements from ArrayList in Java?
- Python Program to Swap the First and Last Value of a List
- Golang program to add elements at first and last position of linked list
- Interchange elements of the first and last rows in the matrix using Python
- How to get the last element of a list in Python?
- Python How to get the last element of list
- Python Program to add element to first and last position of linked list
