I have a function called pop_item() that I am trying to make act like a pop() that works with the list class, how would I do that, here is my code: def empty(lst): return lst == []
def pop_item(lst):
lst = lst[::-1]
new_lst = []
for i in lst:
if i != lst[-1]:
new_lst += [i]
lst = new_lst
return lst
def main():
todo = [1,2,3,4]
print(todo) #prints [1,2,3,4] as expected
print('\n' + 75 * '_' + '\n')
print(pop_item(todo)) #pop the item off
print(todo) #output should then be [1,2,3]
if __name__ == '__main__':
main()
Note: I am not allowed to use any built in functions eg len(),index,del() etc.
Here are a few variants of popping items from a list without using
listfunctions, only slicing & list comprehensionpop the last item using slicing & slicing assignment:
pop a valued item (last one but can be a parameter) by rebuilding the list & slice assign
(that can pop other items in the list if they have the same value)
by speciftying item position/index in the list: