Open In App

Get first and last elements of a list in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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 element of list
res = [test_list[0]]
 
# printing result
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




# 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 last element of list
res = [test_list[-1]]
 
# printing result
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




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

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




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

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




# 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


Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads