Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to remove last 2 elements from list in python

l = list(range(1,5))
l = test_list[: len(test_list) - 2] 
Comment

python slice last 2 items of list

# Last two items of ENTIRE list
ls = [1, 2, 3, 4, 5, 6, 7, 8]
print(ls[-2:])
# [7, 8]
# equivalent to ls[len(ls)-2:len(ls)], see below

# Last two items of A PART of list
cutoff = 5
print(ls[cutoff-2:cutoff])	# where -2 is last 2 values
# [4, 5]

# Using a for loop
for i in range(0, len(ls)):
   if i+1 ==  len(ls)):
      print(ls[i-1:len(ls)])
# [7, 8]
Comment

PREVIOUS NEXT
Code Example
Python :: DateEntry tkinter 
Python :: show only integer values matplotlib 
Python :: pil saves blue images 
Python :: cmap perlin noise python 
Python :: godot get scenes from folder 
Python :: pandas redondear un valor 
Python :: convert pdf to excel python 
Python :: python concatenate list of lists 
Python :: python append list to list 
Python :: expanding nebula foobar 
Python :: get last save id django model 
Python :: Access python http.server on google colab 
Python :: check how many days old file is python 
Python :: alphabetical 
Python :: classes in python 
Python :: get value of property of object with name python 
Python :: The Python package manager (pip) can only be used from outside of IPython. 
Python :: python ascii() 
Python :: change period to timestamp python 
Python :: add text to jpg python 
Python :: re module python 
Python :: displaying data from this column where value is this python 
Python :: python remove table widget numbers 
Python :: least recently used cache 
Python :: python send email from icloud 
Python :: insert value in string python 
Python :: 151 - Power Crisis solution 
Python :: how to create tupple in python 
Python :: convert png rgba to rgb pyhton 
Python :: convert to string in python 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =