How to remove the value from a list and change it at the same index with another value?

Viewed 25

I want to update the board to add the player input and remove the value at the position index and add a new value:

def place_marker(board, marker, position): 
    # Anything false than being in acceptable_values
    index_position = 'Wrong'
    #Keep Asking while the input not in acceptable_values
    while index_position not in ["1",'2','3','4','5','6','7','8',"9"]:     
        index_position = input('Choose a number from 1 to 9: ')     
        if index_position not in ["1",'2','3','4','5','6','7','8',"9"]:
            print('Invalid Input')
        
    position = int(index_position) 
    board.pop(position)
1 Answers

To change the value of a list at the same place you just need to reference it by its index like this:

my_list = [1,2,3,4,5]

my_list[3] = 100   # reference the 4th item (list starts at 0 index)

print(my_list)

returns:

[1, 2, 3, 100, 5]
Related