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

  1. len() -It is used to find the length of the array. 
  2. list = [1,2,3,4] 
  3. n=1 
  4. list = list[:len(list)-n] 
  5. print(list) 

method 2: Using negative slicing

  1. list = [1,2,3,4] 
  2. n=1 
  3. list = list[:-n] 
  4. print(list)

Comments

Popular posts from this blog

proassignment help backlink data index 41

programming shark backink data index 31

Programming shark backlink data index 67