Want to remove an element at a given position
i in a list
Simply use the
del a[i] operation to remove the element at index position
Here’s the Python code example:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] del a print(a) # [0, 1, 3, 4, 5, 6, 7, 8, 9] del a print(a) # [1, 3, 4, 5, 6, 7, 8, 9] del a[-1] print(a) # [1, 3, 4, 5, 6, 7, 8] del a[2:4] print(a) # [1, 3, 6, 7, 8] del a[::-1] print(a) # 
This is the easiest way of removing a list element at a certain position. You can even use slicing to remove a whole subsequence of the list. For example, the slice operation
del a[2:4] removes the elements with indices 2 and 3 from the list
If you lack slicing skills, check out my “Coffee Break Python” book package (with free bonus Python slicing book).
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.