Python Slice Remove First and Last Element from a List

Problem Formulation

πŸ’¬ Question: Given a Python list stored in a variable lst. How to remove the first and last elements from the list lst?

Example: The list ['Alice', 'Bob', 'Carl', 'Dave'] stored in variable lst becomes ['Bob', 'Carl'].

Method 1: Slicing List[1:-1]

To remove the first and last elements from a Python list, use the expression lst = lst[1:-1] that uses a Python feature called slicing with the syntax variable[start:stop] to iterate over a sequence starting from the start index (included) and ending in the element at stop index (excluded). If stop is a negative integer such as -i, Python takes the i-th right-most element.

Here’s an example:

lst = ['Alice', 'Bob', 'Carl', 'Dave']
lst = lst[1:-1]
print(lst)
# ['Bob', 'Carl']

This code snippet removes the first element 'Alice' and the last element 'Dave' from the list.

Note that this approach also works for lists with one element:

lst = ['Alice']
lst = lst[1:-1]
print(lst)
# []

… and also for an empty list with zero elements:

lst = []
lst = lst[1:-1]
print(lst)
# []

Feel free to get some background on slicing by watching the following tutorial:

🌎 Learn More: An Introduction to Python Slicing

Method 2: Slicing List Without Negative Index

To remove the first and last elements from a Python list, you can also use the slightly more complex expression lst = lst[1:len(lst)-1] that assigns the result of the slicing operation to the list and, thereby, overwrites the original longer list. We decrement the len() function result to obtain the index of the last element that is excluded from the slice.

Here’s an example:

lst = ['Alice', 'Bob', 'Carl', 'Dave']
lst = lst[1:len(lst)-1]
print(lst)
# ['Bob', 'Carl']

You can watch my related tutorial video:

🌎 Learn More: The Python len() function

Method 3: list.pop() and list.pop(0)

To remove the first element of a Python list, you can use the list.pop(0) method. To remove the last element of a Python list, you can use the list.pop() method without argument. You can call both to remove the first and the last elements if the list has at least two elements.

Here’s a minimal example:

lst = ['Alice', 'Bob', 'Carl', 'Dave']
lst.pop() # remove last
lst.pop(0) # remove first
print(lst)
# ['Bob', 'Carl']

However, for a list with less than two elements, this will raise an IndexError: pop from empty list. A simple if check can make sure that the list has at least two elements—and otherwise simply override it with the empty list.


Here’s some background info in case you want to dive deeper into this approach:

πŸ’‘ The list.pop() method removes and returns the last element from an existing list. The list.pop(index) method with the optional argument index removes and returns the element at the position index.

🌎 Learn More: The Python list.pop() method

Summary

You’ve learned the three easy ways to remove both the first and last elements from a Python list:

  • Method 1: Slicing list[1:-1]
  • Method 2: Slicing List Without Negative Index list[1:len(list)-1]
  • Method 3: list.pop() and list.pop(0)

Thanks for your interest in learning with Finxter! You can check out our free email academy here:


πŸ‘‰ Related Tutorials: ‡️