How can I delete the last n elements from an array in Python?
Python: Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
How can I delete the last n elements from an array in Python?
method 1: Using len() + list slicing
- len() -It is used to find the length of the array.
- list = [1,2,3,4]
- n=1
- list = list[:len(list)-n]
- print(list)
method 2: Using negative slicing
- list = [1,2,3,4]
- n=1
- list = list[:-n]
- print(list)
Comments
Post a Comment