How do I remove the first item from a list?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
When working with lists in programming, a common task you might encounter is the need to remove the first item from a list. This manipulation can be crucial for a variety of reasons, such as processing queues where the first element is to be served first, or simply modifying data during runtime. This article explores several methods to remove the first item from a list, covering popular programming languages such as Python, JavaScript, and Java.
Python: Using pop() and slicing
Python lists are dynamic arrays that provide a rich set of methods to perform various operations including removing elements.
- Using the
pop()method: Thepop()method can be used to remove an item at a specified position in the list. By default, if no index is specified,pop()removes and returns the last item in the list. To remove the first item, you pass0as an index.
- Using list slicing: List slicing is another powerful feature in Python, which can be used to remove items. To remove the first item, you can slice the list from the second item to the end.
JavaScript: Using shift() method
In JavaScript, arrays provide the shift() method specifically designed to remove the first element from an array and return that removed element. This method changes the length of the array.
Java: Using remove() from ArrayList
In Java, arrays are static in size, but the ArrayList class provides dynamic array capabilities, including methods to add and remove elements.
Considerations and Best Practices
- Performance: Be aware of the performance of removing elements from the beginning of arrays, especially in large lists. For some languages or implementations, this may require shifting all other elements in the memory, which can be a costly operation (
O(n)time complexity). - Immutability: In some functional programming scenarios or when the immutability of data structures is a priority (such as in Redux state management in JavaScript), you might want to avoid methods that mutate the array directly like
pop(),shift(), orremove(). Instead, prefer slicing or equivalent methods that return a new array or list. - Error Handling: Always check if the list is not empty before trying to remove an element to prevent errors or exceptions from being thrown.
Summary Table
| Language | Method | Description | Mutates List? |
| Python | pop(0) | Removes the first element, returns its value | Yes |
| Python | list[1:] | Creates a new list without the first element | No |
| JavaScript | shift() | Removes the first element, returns its value | Yes |
| Java | ArrayList.remove(0) | Removes the first element of ArrayList, return its value | Yes |
Understanding how to manipulate lists efficiently can drastically affect performance and readability of code. Whether you're managing data in Python, handling state in JavaScript, or operating on collections in Java, knowing how to remove the first item from a list is an essential skill in a developer's toolkit.
Related reading
- How do I remove the first item from a list?
- How do I return dictionary keys as a list in Python?
- How do I return dictionary keys as a list in Python?
- How do I return the index of the target element in a Python array?
- How do I remove whitespace from the end of a string in Python?
- How do I remove/delete a virtualenv?
- How do I reverse a list or loop over it backwards?
- How do I reverse a list or loop over it backwards?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.