- Prepare
- Python
- Basic Data Types
- Lists
- Discussions
Lists
Lists
+ 0 comments if name == 'main': N = int(input()) lst = [] for i in range (N): cmd = input().split() if cmd[0] == 'insert': lst.insert(int(cmd[1]),int(cmd[2])) elif cmd[0] == 'print': print(lst) elif cmd[0] == 'remove': lst.remove(int(cmd[1])) elif cmd[0] == 'append': lst.append(int(cmd[1])) elif cmd[0] == 'sort': lst.sort() elif cmd[0] == 'pop': lst.pop() elif cmd[0] == 'reverse': lst.reverse() else: pass
+ 0 comments if name == 'main':
N = int(input()) ls = list() while N>0: N -=1 command = input().split() if command[0] == "insert": ls.insert(int(command[1]),int(command[2])) elif command[0] == "remove": ls.remove(int(command[1])) elif command[0] == "print": print(ls) elif command[0] == "sort": ls.sort() elif command[0] == "append": ls.append(int(command[1])) elif command[0] == "pop": ls.pop() elif command[0] =="reverse": ls = ls[::-1] else: pass
+ 0 comments if cmd[0] == 'insert': lst.insert(int(cmd[1]), int(cmd[2])) elif cmd[0] == 'remove': lst.remove(int(cmd[1])) elif cmd[0] == 'append': lst.append(int(cmd[1])) elif cmd[0] == 'sort': lst.sort() elif cmd[0] == 'pop': lst.pop() elif cmd[0] == 'reverse': lst.reverse() elif cmd[0] == 'print': print(lst)
+ 0 comments A shorter solution using the eval function:
if __name__ == '__main__': l = [] N = int(input()) for i in range(N): data = input().split() match (data[0], len(data)): case ('print', 1): print(l) case _: eval('l.'+data[0]+'('+','.join(data[1:])+')')
+ 1 comment could you anyone pleasee tell me... whats wrong in my code ? **my code ** n = int(input()) l = [] for i in range(n): sur = input().split() surya = sur[0] if surya == "insert": l.insert(int(sur[1]),int(sur[2])) elif surya == "print": print(l) elif surya == "remove": l.remove(int(sur[1])) elif surya == "append": l.append(int(sur[1])) elif surya == "sort": l.sort() elif surya == "pop": l.pop() elif surya == "reverse": l.reverse() break print(l)
Sort 2331 Discussions, By:
Please Login in order to post a comment