• + 0 comments

    Hi guys, This is the solution I thought of. Here, the commands must be taken iteratively as input. for that, we can use input().split() after a for loop, where the split() command stores the command line entered as a list!

    if command is stored as list, for example, ['insert', '0'] command would mean insert operation with 0 value. so we assign the 0th position of the command line as operation. then we convert the value to integer as the value to be added to new list is a number!

    here is the code:

    Spoiler

    if __name__ == '__main__':
        N = int(input())
     
        a = list()
    
        for i in range(N):
            cmd = input().split()
            operation = cmd[0] 
            
            if operation=="insert":
                a.insert(int(cmd[1]), int(cmd[2]))
            elif operation=="remove":
                a.remove(int(cmd[1]))
            elif operation=="append":
                a.append(int(cmd[1]))
            elif operation=="sort":
                a.sort()
            elif operation=="pop":
                a.pop()
            elif operation=="reverse":
                a.reverse()
            elif operation=="print":
                print(a)