Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I implement a function that undoes the last input that modified a list? So I recently made a program that adds, removes (either just plain removes or by a user input [i.e. if an item is less than a user input then it gets deleted and the such]) and modifies packages (dictionaries containing beginning and end dates, destination and price) in a list (I already asked for help here once)
When I implemented the remove/modify functions, I also made sure to append the original item to a new list so that when I had to call the undo function it'd just append the original item to the original list again, however, the function just deletes the item that was in the list after the remove/modify calls and just appends the item that got removed/modified
What I tried to do (my train of thought) was that I wanted to create a new list called "lst" where I stock the items that were about to be removed, and I would essentially use it in the undo function (basically the function that you call undo list). In the undo function I enumerate thru l and lst, and I tried to append the item from the lst list. Now I see how I messed up given that the index will probably not be the same and I caught an IndexError so I resorted to popping the previous item

the code: https://pastebin.com/taZAjhQf

What I have tried:

this is the aforementioned undo function, the whole code is very long
def undo(l,lst):
    for el,pachet in enumerate(l):
        l.append(lst[el])
        l.pop()

this is the whole code:
the code: https://pastebin.com/taZAjhQf 
Posted
Updated 19-Oct-21 21:20pm

1 solution

For undo/redo operation, the command design pattern is frequently used. Have a look at https://medium.com/design-patterns-in-python/undo-redo-pattern-in-python-70ade29644b3[^].
 
Share this answer
 
Comments
Andrei Iacob 20-Oct-21 3:36am    
I'll check it out, thanks
I also tried it with deepcopy during this time, but I found out that I can't use it for this assignment :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900