I've written a code that simply takes a few commands from the standard input and tries to modify a list based on the commands. The user can pop the last element of the list, insert a new value, append, sort the list, and so on. The problem I am facing, is that the code modifies the list and prints the FINAL result after going through ALL the commands. But I want the list modification printed precisely for EACH command one after another!
Here's my code:
import re
lst = [5,1,6,2,6,2,9,12,1,5]
def insert_element(index, value, lst):
lst.insert(index, value)
return lst
def remove_element(element, lst):
lst.remove(element)
return lst
def append_element(value, lst):
lst.append(value)
return lst
def sort_list(lst):
lst = sorted(lst)
return lst
def pop_element(lst):
lst.pop()
return lst
def reverse_list(lst):
lst = lst[::-1]
return lst
commands = []
results = []
queries = int(input())
for i in range(queries):
commands.append(input())
for j in commands:
if re.match(r'insert*', j):
results.append(insert_element(int(j[7]), int(j[9]), lst))
if re.match(r'remove*', j):
results.append(remove_element(int(j[7::]), lst))
if re.match(r'append*', j):
results.append(append_element(int(j[7::]), lst))
if re.match(r'sort*', j):
results.append(sort_list(lst))
if re.match(r'pop*', j):
results.append(pop_element(lst))
if re.match(r'reverse*', j):
results.append(reverse_list(lst))
if re.match(r'print*', j):
results.append(lst)
for z in results:
print(z)
SAMPLE INPUT
4
print
pop
append 3
insert 2 9
EXPECTED OUTPUT
[5,1,6,2,6,2,9,12,1,5]
[5,1,6,2,6,2,9,12,1]
[5,1,6,2,6,2,9,12,1,3]
[5,1,5,6,2,6,2,9,12,1,3]
CODE'S OUTPUT
[5,1,5,6,2,6,2,9,12,1,3]
[5,1,5,6,2,6,2,9,12,1,3]
[5,1,5,6,2,6,2,9,12,1,3]
[5,1,5,6,2,6,2,9,12,1,3]
How can I make it get the expected output rather than this?! I really appreciate any help.