I am trying to make a function that shortens lists to only 4 elements. However, I am unable to assign a type to the argument of the function, which I have called my_list
.
I tried using type()
, but that did not work.
Here is some code:
LL = [2,3,4,5,6,2,5,4]
print(len(LL))
def remove_last_elements(the_list):
the_list.type(list) #Here it gives an error message that I will put below
for i in range (len(the_list)-4):
the_list=the_list.remove(the_list[len(the_list)-1])
remove_last_elements(LL)
print(LL)
It gives me this error message. If I use .type()
.
remove_last_elements(LL)
File "C:\Users\visha\OneDrive\Desktop\Arnav's Things\Coding\Python Programs (and side-products)\Balloons.py", line 5, in remove_last_elements
the_list.type(list)
AttributeError: 'list' object has no attribute 'type'
Here is another segment of code without the .type()
thing:
LL = [2,3,4,5,6,2,5,4]
print(len(LL))
def remove_last_elements(the_list):
for i in range (len(the_list)-4):
the_list=the_list.remove(the_list[len(the_list)-1])
remove_last_elements(LL)
print(LL)
Which gives the error message below:
remove_last_elements(LL)
File "C:\Users\visha\OneDrive\Desktop\Arnav's Things\Coding\Python Programs (and side-products)\Balloons.py", line 5, in remove_last_elements
the_list=the_list.remove(the_list[len(the_list)-1])
AttributeError: 'NoneType' object has no attribute 'remove'