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)) |
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)) |
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)) |
The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Recommended Posts:
- Python | Sorting list of lists with similar list elements
- Python | Add list elements with a multi-list based on index
- Python | Replace elements in second list with index of same element in first list
- Python | Indices of sorted list of list elements
- Python | Maximum sum of elements of list in a list of lists
- Python | Get last N elements from given list
- Python | List frequency of elements
- Python | Adding two list elements
- Python | Remove last K elements of list
- Python | Merge list elements
- Python | Check if all elements in a List are same
- Python | Segregate list elements by Suffix
- Python program to interchange first and last elements in a list
- Python | Get positive elements from given list of lists
- Python | Check if list contains all unique elements
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.



