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. Show
Method #1 : Using list index
Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4]
Output: The original list is : [1, 5, 6, 7, 4] The first and last element of list are : [1, 4] Article Tags : Python Python Programs Python list-programs Reading the First and Last Elements of a Python ListElements in a list are numbered zero onwards. In this tutorial, we will learn different ways to access the first and the last elements from a list. Let’s start by initializing a list. 1. Initialize a Python listTo initialize a list in Python use: a = [6, 5, 9, 7] This will initialize a new list ‘a‘ with the four elements we have mentioned. 2. Accessing list elementsYou can access elements from the list by using the name of the list along with an index number. To print the first element use: print(a[0]) To print the last element use : print(a[-1]) Using -1 as the index gives us the last element from the list. Python Code to Get First and Last Element of a ListThe complete code is as follows : a = [6, 5, 9, 7] print("first element is" ,a[0]) print("last element is", a[-1]) Output Understand Slice Notation – PythonBefore you learn how to get the first element of a list in Python, you should understand slice notation in Python. As per Python documentation, you need to think that indices (both positive and negative indexes) are pointing between characters as depicted in the following diagram. For more details and examples. Please check more details on Understand Slice Notation in Python. Get the last element of a list using indexing in PythonIndexing in python is a way to access elements from a list. In python, we can use positive indices as well as negative indices. Positive indices start with zero which corresponds to the first element of the list and the last element of the list is identified by the index “listLen-1” where “listLen” is the length of the list. Alternatively, negative indices start from -1 which corresponds to the last element of the list. It goes till “-listLen” where listLen is the length of the list. The index “-listLen” corresponds to the first element in the list. To get the last element of a list, we can first find the length of the list using the len() function. Then we can access the last element of the list at the index “listLen-1” as follows. myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) listLen = len(myList) lastElement = myList[listLen - 1] print("Last element of the list is:", lastElement)Output: Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7Alternatively, we can use negative indexing to access the last element of the list. The last element of the list is at index -1 which can be accessed as follows. myList = [1, 2, 3, 4, 5, 6, 7] print("Given List is:", myList) lastElement = myList[- 1] print("Last element of the list is:", lastElement)Output: Given List is: [1, 2, 3, 4, 5, 6, 7] Last element of the list is: 7We can see that while using negative indexing, we don’t have to calculate the length of the list. Python Program to Interchange First and Last Elements in listIn this tutorial, you will learn how to interchange the first and the last elements in a list in Python. Lists in Python are a group of ordered values that are closed within a square[] bracket. For a given list, we have to write a Python program that will swap the first and the last elements of a list. Swapping means exchanging the values of the two numbers. Suppose you have two variables a and b. The value of a is 40 and the value of b is 30. After swapping the value of a will become 30 and the value of b will become 40. For a list a[ ]= [1, 2, 3, 4, 5], swap elements at position 1 and position 5 To get the final list as a[]=[5,2,3,4,1] 1. Using list.pop() functionThe simplest approach is to use the list’s pop([i]) function, which removes an element present at the specified position in the list. If we don’t specify any index, pop() removes and returns the last element in the list.
DownloadRun Code
|