Skip to content
Related Articles

Related Articles

Python | Get first and last elements of a list

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 27 Sep, 2022
View Discussion
Improve Article
Save Article

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




# 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 the whole list to skip to the last element after the first element. 

Python3




# 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 the first and last elements of the list. Naive method of finding is converted to a single line using this method. 

Python3




# 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]

Method #4: Using map and __getitem__ method 

With these methods, we are basically accessing the element at a given position from list. We match one list which contains an element and one list which contains an index of element which we want and map two array and use __getitem__ to get the element from index which is stored in the index list.

Javascript




# Python3 code to demonstrate
# to get first and last element of list
# map and __getitem__ function
 
# initializing list
test_list = [1, 5, 6, 7, 4]
index_list = [0, len(test_list)-1]
# printing original list
print ('The original list is : ' +  str(test_list))
 
# using map and __getitem__
# to get first and last element of list
res =  map(test_list.__getitem__, index_list)
 
# printing result
print ('The first and last element of list are : ' , *res)

Output:

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!