Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Slice Remove First and Last Element from a List
1
Python Slice Remove First and Last Element from a List

5/5 – (1 vote)

Problem Formulation

[Image: 1f4ac.png] Question: Given a Python list stored in a variable
Code:
lst
. How to remove the first and last elements from the list
Code:
lst
?

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

Method 1: Slicing List[1:-1]

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

Here’s an example:

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

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

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

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

… and also for an empty list with zero elements:

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

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



[Image: 1f30e.png] 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
Code:
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
Code:
len()
function result to obtain the index of the last element that is excluded from the slice.

Here’s an example:

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

You can watch my related tutorial video:



[Image: 1f30e.png] Learn More: The Python
Code:
len()
function


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

To remove the first element of a Python list, you can use the
Code:
list.pop(0)
method. To remove the last element of a Python list, you can use the
Code:
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:

Code:
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
Code:
IndexError: pop from empty list
. A simple
Code:
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:

[Image: 1f4a1.png] The
Code:
list.pop()
method removes and returns the last element from an existing
Code:
list
. The
Code:
list.pop(index)
method with the optional argument
Code:
index
removes and returns the element at the position
Code:
index
.



[Image: 1f30e.png] Learn More: The Python
Code:
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
    Code:
    list[1:-1]

  • Method 2: Slicing List Without Negative Index
    Code:
    list[1:len(list)-1]

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

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

Users browsing this thread:
1 Guest(s)


Forum Jump: