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 Python list.
Get the first elements of a list using index
Using the list indices inside the master list can perform this particular task. This is the most naive method to achieve this particular task one can think of to get the first element of the list.
Python3
test_list = [ 1 , 5 , 6 , 7 , 4 ]
print ( "The original list is : " + str (test_list))
res = [test_list[ 0 ]]
print ( "The first element of list are : " + str (res))
|
Output
The original list is : [1, 5, 6, 7, 4]
The first element of list are : [1]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using defaultdict() + loop which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Get the last elements of a list using index
Using the list indices inside the master list can perform this particular task. This is the most naive method to achieve this particular task one can think of to get the last element from the list.
Python3
test_list = [ 1 , 5 , 6 , 7 , 4 ]
print ( "The original list is : " + str (test_list))
res = [test_list[ - 1 ]]
print ( "The last element of list are : " + str (res))
|
Output
The original list is : [1, 5, 6, 7, 4]
The last element of list are : [4]
Time Complexity: O(1), where it take constant time
Auxiliary Space: O(1), as we’re using constant additional space
Get first and last elements of a list using List slicing
One can also make use of the list-slicing technique to perform the particular task of getting the first and last element. We can use the step of the whole list to skip to the last element after the first element.
Python3
test_list = [ 1 , 5 , 6 , 7 , 4 ]
print ( "The original list is : " + str (test_list))
res = test_list[:: len (test_list) - 1 ]
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]
Get first and last elements of a list 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. The naive method of finding is converted to a single line using this method.
Python3
test_list = [ 1 , 5 , 6 , 7 , 4 ]
print ( "The original list is : " + str (test_list))
res = [test_list[i] for i in ( 0 , - 1 )]
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]
Time complexity: O(n), where n is length of test_list.
Auxiliary Space: O(1)
Get first and last elements of a list using map and __getitem__ method
With these methods, we are basically accessing the element at a given position from the list. We match one list which contains an element and one list which contains an index of elements that we want and map two lists and use __getitem__ to get the element from the index which is stored in the index list.
Python3
test_list = [ 1 , 5 , 6 , 7 , 4 ]
index_list = [ 0 , len (test_list) - 1 ]
print ( 'The original list is : ' + str (test_list))
res = map (test_list.__getitem__, index_list)
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!