How do I find the first and last elements of a list?

Python | Get first and last elements of a list

Sometimes, there might be a need to get the range between which a number lies in the list, for such applications we require to get the first and last element of the list. Let’s discuss certain ways to get the first and last element of the list.

Method #1 : Using list index
Using the list indices inside the master list can perform this particular task. This is most naive method to achieve this particular task one can think of.




# Python3 code to demonstrate

# to get first and last element of list

# using list indexing

# initializing list

test_list = [1, 5, 6, 7, 4]

# printing original list

print ("The original list is : " + str(test_list))

# using list indexing

# to get first and last element of list

res = [ test_list[0], test_list[-1] ]

# printing result

print ("The first and last element of list are : " + str(res))

Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]


Method #2 : Using List slicing
One can also make use of list slicing technique to perform the particular task of getting first and last element. We can use step of whole list to skip to the last element after the first element.




# Python3 code to demonstrate

# to get first and last element of list

# using List slicing

# initializing list

test_list = [1, 5, 6, 7, 4]

# printing original list

print ("The original list is : " + str(test_list))

# using List slicing

# to get first and last element of list

res = test_list[::len(test_list)-1]

# printing result

print ("The first and last element of list are : " + str(res))

Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]


Method #3 : Using list comprehension
List comprehension can be employed to provide a shorthand to the loop technique to find first and last element of the list. Naive method of finding is converted to a single line using this method.




# Python3 code to demonstrate

# to get first and last element of list

# using list comprehension

# initializing list

test_list = [1, 5, 6, 7, 4]

# printing original list

print ("The original list is : " + str(test_list))

# using list comprehension

# to get first and last element of list

res = [ test_list[i] for i in (0, -1) ]

# printing result

print ("The first and last element of list are : " + str(res))

Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]

How do I find the first and last elements of a list?




Article Tags :

Python

Python Programs

Python list-programs

Reading the First and Last Elements of a Python List

Elements in a list are numbered zero onwards. In this tutorial, we will learn different ways to access the first and the last elements from a list.

Let’s start by initializing a list.

1. Initialize a Python list

To initialize a list in Python use:

a = [6, 5, 9, 7]

This will initialize a new list ‘a‘ with the four elements we have mentioned.

2. Accessing list elements

You can access elements from the list by using the name of the list along with an index number. To print the first element use:

print(a[0])

To print the last element use :

print(a[-1])

Using -1 as the index gives us the last element from the list.

Python Code to Get First and Last Element of a List

The complete code is as follows :

a = [6, 5, 9, 7] print("first element is" ,a[0]) print("last element is", a[-1])

Output

How do I find the first and last elements of a list?

Understand Slice Notation – Python

Before you learn how to get the first element of a list in Python, you should understand slice notation in Python.

As per Python documentation, you need to think that indices (both positive and negative indexes) are pointing between characters as depicted in the following diagram.

How do I find the first and last elements of a list?
Note, the first row in the above diagram indicates positive indexes position numbers from o to 6 for the string. And second row indicates the corresponding negative indexes. When you slice the string from index i to index j, it will consists of all the characters between i and j.

For more details and examples. Please check more details on Understand Slice Notation in Python.

Get the last element of a list using indexing in Python

Indexing in python is a way to access elements from a list. In python, we can use positive indices as well as negative indices. Positive indices start with zero which corresponds to the first element of the list and the last element of the list is identified by the index “listLen-1” where “listLen” is the length of the list.

Alternatively, negative indices start from -1 which corresponds to the last element of the list. It goes till “-listLen” where listLen is the length of the list. The index “-listLen” corresponds to the first element in the list.

How do I find the first and last elements of a list?

To get the last element of a list, we can first find the length of the list using the len() function. Then we can access the last element of the list at the index “listLen-1” as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) listLen = len(myList) lastElement = myList[listLen - 1] print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

Alternatively, we can use negative indexing to access the last element of the list. The last element of the list is at index -1 which can be accessed as follows.

myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) lastElement = myList[- 1] print("Last element of the list is:", lastElement)

Output:

Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7

We can see that while using negative indexing, we don’t have to calculate the length of the list.

Python Program to Interchange First and Last Elements in list

In this tutorial, you will learn how to interchange the first and the last elements in a list in Python. Lists in Python are a group of ordered values that are closed within a square[] bracket. For a given list, we have to write a Python program that will swap the first and the last elements of a list. Swapping means exchanging the values of the two numbers. Suppose you have two variables a and b. The value of a is 40 and the value of b is 30. After swapping the value of a will become 30 and the value of b will become 40.

For a list a[ ]= [1, 2, 3, 4, 5], swap elements at position 1 and position 5

How do I find the first and last elements of a list?

To get the final list as a[]=[5,2,3,4,1]

How do I find the first and last elements of a list?

1. Using list.pop() function

The simplest approach is to use the list’s pop([i]) function, which removes an element present at the specified position in the list. If we don’t specify any index, pop() removes and returns the last element in the list.

1

2

3

4

5

6

7

if __name__ == '__main__':

l = list(range(1, 5))

print(l)# [1, 2, 3, 4]

l.pop()

print(l)# [1, 2, 3]

DownloadRun Code


The pop([i]) function raises an IndexError if the list is empty as it tries to pop from an empty list.