In this tutorial, we will look at how to remove the first element from a Numpy array with the help of some examples.
How do I remove the first element of a Numpy Array?
You can use the numpy delete()
function to remove the first element of a numpy array. Pass 0
as the index of the element to be removed. The following is the syntax –
# remove first element from numpy array ar, assuming numpy imported as np np.delete(ar, 0)
The np.delete()
function is used to remove an element using its index. Since we want to remove the first element, we pass 0
. It returns a copy of the original array with the specific element deleted.
Alternatively, you can also slice the original array starting from the index 1
to the end of the array to get a new numpy array with the first element removed from the original array.
Note that numpy arrays are immutable. That is, they cannot be modified after creation.
Steps to remove the first element in Numpy
Let’s now look at a step-by-step example of using the methods mentioned above –
Step 1 – Create the numpy array
First, we will create a numpy array that we will be using throughout this tutorial.
import numpy as np # create numpy array ar = np.array([10, 20, 30, 40, 50]) # print the array print(ar)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
[10 20 30 40 50]
Here we use the np.array()
function to create a numpy array from a list of numbers. We now have a numpy array containing the numbers 10, 20, 30, 40, and 50.
Step 2 – Remove the first element from the array
First, let’s use the np.delete()
function to remove the first element of the numpy array ar
created above.
# remove first element ar_new = np.delete(ar, 0) # print the returned array print(ar_new) # print the original array print(ar)
Output:
[20 30 40 50] [10 20 30 40 50]
The resulting array does not have the first element from the original array. You can also see that the original array is not modified.
Let’s now use the slicing method to remove the first element.
# remove first element ar_new = ar[1:] # print the returned array print(ar_new) # print the original array print(ar)
Output:
[20 30 40 50] [10 20 30 40 50]
Here, we slice the original array from the index 1
to the end of the array. We get the same result as above. A copy of the original array with the first element removed.
For more on slicing numpy arrays, refer to numpy’s guide on indexing and slicing.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.