0

How would I be able to replace a single index in a 2d list if we don't know what value is currently in that location?

Initially, i thought

twoDList[0][0].replace('A')

would work; however, it does not. So what is the proper way to replace an index in a 2d list in python?

3
  • ` #Craft a 2d board board = [['.' for a in range(rowNum)] for b in range(columnNum)] #Iterate through each row for i in range(rowNum): #Iterate through each column for j in range(columnNum):: board[i][j].replace('A') ` rowNum and columnNum are user inputed constants Commented Nov 10, 2015 at 23:50
  • I don't understand if you want to insert A and you don't care about the old value or you want to only replace a value the value in 2d list if it is A? Commented Nov 10, 2015 at 23:59
  • I would like to replace the old value with A Commented Nov 11, 2015 at 0:17

1 Answer 1

1

.replace does not do in place replacements since strings are immutable, rather it returns a copy of the string. make sure to set the new value to the item:

tw0dList[0][0] = 'A'
Sign up to request clarification or add additional context in comments.

2 Comments

i still get the error TypeError: replace() takes at least 2 arguments (1 given)
i wasn't even thinking, i relooked at your question and it looks like you dont want to be using replace. correct me if im wrong but do you just want the value at twodlist[0][0] to be 'A'? i editted it to do that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.