Python

Python Pop Last Element from List

A list is a collection of indexed and sorted values, similar to an array. It’s reorderable so that we can rearrange the components in a list. It holds a comma-separated list of any data item enclosed in a square bracket. Each list entry has an index that ranges from zero to infinity.

To delete an item from a list, you can utilize the list data type in Python in various ways. The list pop() method in Python removes the final value and returns it from a List or the index value provided. The Python List data type allows you to sort objects of various data kinds logically. Commas divide the values, which are enclosed in square brackets ([]) and (,). You must pass the element’s index in order to remove it from the list. The index starts at zero.

To get the first element from the list, set the index to 0. You can use the index as -1 to eliminate the last entry. The index argument is not required. The default value is -1, and the list’s final member returns if no value is supplied. The pop() method returns an error with the message IndexError: pop index if the specified index is nonexistent or out of range. pop(), clear(), and remove() are the popular list methods. You can also remove items using the del keyword in addition to the above-mentioned methods.

Example 1:

We have defined a list called ‘First_List’ in the preceding example. As you can see, each piece is separated by a comma and is enclosed between square brackets, i.e. []. This list includes components of several data types. The item ‘Alex’ is at index 0 positions. We have an integer 55 at the position of index 1. We have a floating-point number of 66.6 at index 2. We can store components of many types in a single list in this manner. Let’s move on to seeing how to pop the last element from a list in Python now that you have a good understanding of how to generate lists. The most often used list object function is the pop() method.

This method returns an item or element that has been removed from the list. To distinguish between the pop() and the remove() method, we must supply the item to be deleted in the remove() method. However, when we use the pop() function, we pass the item’s index as an argument, returning it at the specified index. If the object to be removed is not found, an IndexError will be thrown. Consider the following example. Here the pop() method is used with the input ‘1’, which is just the index position 7. The pop() function, as you can see from the result, eliminates the integer ’30’, which is the list’s last element.

First_List = ["Alex",55,66.6,77,"Rebecca",10,20,30]

First_List.pop(7)

print(First_List)

https://lh3.googleusercontent.com/PD7mdgk7RspsFsCbd7wyHy-PJZ-NGCI1_8zxma20b_hVS1tODtNTH4gxDozWSRJ8lCvd1fIMb9DQ8jbWQj5BU1YUeBv24nm6wBssPLJ7Lzv_G1lfufUk3XvrvTYHV1iodoGwzpTL

Here you can see the output where the last element is successfully removed.

https://lh6.googleusercontent.com/4LMZKO6KkTLq1gYxQF8B-7cfgRqQyk84Pv8dxsEWWkjKCZxCFOPBqsnTHDGG_Pcqfe4vkeMjntr3NdGB9EBNUYioGaon_kqFTsoJK-ZNuUk0lofNhKDtSo1svy0t9bX3sooC-wfx

Example 2:

In our second example, we have first declared a list with the name ‘mylist’ in the first line of code. After that, we applied the pop method and displayed the item to be removed along with the remaining list items. When we first applied the pop function, we did not identify the index position, but in the last line of code, the index position is passed to the pop method. Here it is 0, which means we want to delete the element from the 0-index position. As you can see in the output, the item in the 0-index position is displayed, and then the remaining items of the list.

mylist = [ 11, 12, 13, 14, 15, 16 ]

print(mylist.pop(), mylist)

print(mylist.pop(0), mylist)

https://lh6.googleusercontent.com/ddTMWtHxyMDXcoZ0G5l47IJPUMNezeH2hNLx7VZ_D3y_lmCegFgjpW1SAwYyKb4QOka5hY_mzrsKlsJsKBgCHqu6pjgMHhoTCjvG8QI-WWdvFDqScsmGU4SxjuGDsMYUNTzukI-v

You get the following output when running the above-mentioned piece of code.

https://lh3.googleusercontent.com/S7sGUcsBEWEbj-r650kToaLLpPDvF-HDtXbvwXAWmFMNbAhwiCeoV8ZVry6HUQC1hgV-F9Czdyx6zPeGj9O2-jODVMOLXTKAdIs0EZkGUSjKwNPl0QGaaPlitUGAXR4p0P5psB3x

Example 3:

In our last example, we will remove an element from the 8th position. Using the pop() method, the element at position 8 is eliminated. Because there is no element at this point in the output, the python interpreter throws an IndexError. This is a quick strategy because it is simple and does not require browsing through the list for an item.

First_List = ["Alex",55,66.6,77,"Rebecca",10,20,30]

First_List.pop(8)

print(First_List)

https://lh5.googleusercontent.com/YFrpGzwVEaZIuKyiARcWec42qohMQwkoC9nKizcKqFleUXDFRAXyK-YkWpiszeJb6gq2C9spVFi4rmUEcrwhFORcajJnvMb3LNxKN8S1mZMhkYYYgk8EPDYThEUSxHAZCRv2U04-

When you run the code above, you will get the error.

https://lh5.googleusercontent.com/spM4LFLl6vDG-Ciykjs0BG1QiZhEdpvU2wV2QJoYvNf_9fYipYzZyfPhBbjLo6eAZmaVc3ysyq1KoOZKQrgDpm9gxFHZazEtHgKNBeZDqQ4BUz0NxxV1V_lH4VTQ2JN_0SMNQQmu

Conclusion:

You know how to delete the final element from a relevant list in Python. Different examples are mentioned with a proper explanation for you to understand and apply the concepts in your programs. We have provided you with a detailed guide on using the pop method to delete the last element or even a specific element from a list.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content