1
if __name__ == '__main__':
    N = int(input())
    my_list = []  # Initialize an empty list to perform operations on.

    for _ in range(N):
        command = input().split()  # Read the command as a space-separated string.
    
        if command[0] == 'insert':
            my_list.insert(int(command[1]), int(command[2]))
        elif command[0] == 'print':
            print(my_list)
        elif command[0] == 'remove':
            my_list.remove(int(command[1]))
        elif command[0] == 'append':
            my_list.append(int(command[1]))
        elif command[0] == 'sort':
            my_list.sort()
        elif command[0] == 'pop':
            my_list.pop()
        elif command[0] == 'reverse':
            my_list.reverse()

#ChatGP isn't explaining it properly, I want to understand the commands 0,1,2 and how they work

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 02 '23 at 09:25

2 Answers2

0

command is array (list) of strings.

if given input is "insert 0 5" then

command = ["insert", "0", "5"]

so command[0] is first element of command (command[0] = "insert"), command[1] is second etc.

these numbers in square brackets are called indexes

more about it: https://www.freecodecamp.org/news/python-find-in-list-how-to-find-the-index-of-an-item-or-element-in-a-list/

Muz
  • 1
  • 2
0

I see what problem you are facing with. The code the first element of the command list (command[0]) to determine what operation to perform.

    if command[0] == 'insert':
        my_list.insert(int(command[1]), int(command[2]))
    elif command[0] == 'print':
        print(my_list)
    elif command[0] == 'remove':
        my_list.remove(int(command[1]))
    elif command[0] == 'append':
        my_list.append(int(command[1]))
    elif command[0] == 'sort':
        my_list.sort()
    elif command[0] == 'pop':
        my_list.pop()
    elif command[0] == 'reverse':
        my_list.reverse()
  • If command[0] is 'insert', it inserts an integer into my_list at the specified index.
  • If command[0] is 'print', it prints the current contents of my_list.
  • If command[0] is 'remove', it removes the first occurrence of a specified integer from my_list.
  • If command[0] is 'append', it appends an integer to the end of my_list.
  • If command[0] is 'sort', it sorts my_list in ascending order.
  • If command[0] is 'pop', it removes and returns the last element of my_list.
  • If command[0] is 'reverse', it reverses the order of elements in my_list. Each command going to effect to list. For example if user enters 1 for N. Then enter insert 0 23 for command, it will insert the integer 23 at index 0 of my_list.

Result

Final code:

if __name__ == '__main__':
N = int(input("How many command do you want to enter: "))
my_list = [1, 8, 9, 31, 25, 65]  # we can add some elements

for _ in range(N):
    command = input("Enter your commands: ").split()  # Read the command as a space-separated string.

    if command[0] == 'insert':
        my_list.insert(int(command[1]), int(command[2]))
    elif command[0] == 'print':
        print(my_list)
    elif command[0] == 'remove':
        my_list.remove(int(command[1]))
    elif command[0] == 'append':
        my_list.append(int(command[1]))
    elif command[0] == 'sort':
        my_list.sort()
    elif command[0] == 'pop':
        my_list.pop()
    elif command[0] == 'reverse':
        my_list.reverse()

print(my_list)