Create an account


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


? 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:




https://www.sickgaming.net/blog/2022/07/...om-a-list/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] 5 Expert-Approved Ways to Remove Unicode Characters from a Python Dict xSicKxBot 0 15 11 hours ago
Last Post: xSicKxBot
  [Tut] 4 Best Ways to Remove Unicode Characters from JSON xSicKxBot 0 25 12-03-2025, 03:06 AM
Last Post: xSicKxBot
  [Tut] List Comprehension in Python xSicKxBot 0 2,107 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 1,956 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,556 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,697 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 1,608 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,512 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 1,534 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] Python | Split String and Remove newline xSicKxBot 0 1,293 12-16-2022, 10:38 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016