In this tutorial, we will look at how to remove the first element from a tuple in Python with the help of some examples.
How to remove the first element from a tuple?
Tuples are immutable. That is, they cannot be altered after creating them. You can, however, create a copy of the tuple with the first element removed in Python.
You can use a slice operation to slice only the part of the tuple you want. Let’s look at how to do this with the help of some examples.
Use slice to remove the first element
To remove the first element from a tuple in Python, slice the original tuple from the second element (element at index 1) to the end of the tuple. This will result in a new tuple with the first element removed.
# create a tuple t = (1, 2, 3, 4, 5) # remove first element t = t[1:] print(t)
Output:
(2, 3, 4, 5)
The tuple now has the first element removed.
Please note that we are not modifying the original tuple. Instead, we are creating a new tuple with the first element removed and then assigning it back to the variable storing the original tuple.
Use Lists Instead
If you want to store an ordered collection of objects and are expecting to make some changes to this collection (for example, adding, removing, replacing elements, etc.), you should prefer using lists that are much more flexible than tuples.
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.
For example, you can use the list pop()
function to remove an element from a list using its index.
# create a list ls = [1, 2, 3, 4, 5] # remove first element ls.pop(0) print(ls)
Output:
[2, 3, 4, 5]
Here, we used the list pop()
function to remove the first element from a list. For more, refer to our detailed tutorial on removing the first element from a list in Python.
You might also be interested in –
- Remove Last Element From Tuple in Python
- Count Frequency of Element in a Tuple in Python
- Sort a Tuple in Python – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.