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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.