Scripting >> Python >> List >>  Demonstration of list methods

 

Initializes the list and reads Ncmd followed by Ncmd lines of text representing the commands to perform on the list

 

Ncmd = int(raw_input())
L = []
for i in range(1,Ncmd+1):
    S = raw_input()
    commands = S.split()
    if commands[0]=="insert":
        i=int(commands[1])
        e=int(commands[2])
        print "Insert into L integer",e,"at position",i
        L.insert(i,e)
        print L
    elif commands[0]=="append":
        e=int(commands[1])
        print "Append integer",e,"into L"
        L.append(e)
        print L
    elif commands[0]=="remove":
        e=int(commands[1])
        print "Remove integer",e,"from L"
        L.remove(e)
        print L
    elif commands[0]=="print":
        print L
    elif commands[0]=="sort":
        L.sort()
        print L
    elif commands[0]=="pop":
        L.pop()
        print L
    elif commands[0]=="reverse":
        L.reverse()
        print L