0
grid = [ ['x'] * w ] * h
grid[0[2]] = 'a'

This was a test to make a grid-like structure. The thing is, I can't access a nested list because integers can't be subscripted. I've seen problems like this but they aren't a recalling problem. Any suggestions?

3
  • 2
    After you learn to access it you'll have to learn about mutable objects... Just sayin' Commented Apr 1, 2012 at 18:40
  • 1
    What JBernando is probably saying is that you cannot use [...]*n for mutable objects if you plan to modify the string, or else every other instance of that string in your grid will automatically change from under you. To elaborate, strings are effectively immutable in python, so it's not ['x']*w that's the problem, but if rows = [['a','b','c']]*3, and you modify a row, all the other rows will change. Use [['x' for c in range(numCols)] for r in range(numRows)]. Commented Apr 1, 2012 at 18:42
  • 1
    ...addendum: [['x']*3 for c in range(numCols)] also works since the inner list is recreated each time. Commented Apr 1, 2012 at 18:48

1 Answer 1

3

I think you may have meant:

grid[0][2] = 'a'

That would take the first element in 'grid' (a list), and set its 3rd element (at position 2) to 'a'

Sign up to request clarification or add additional context in comments.

Comments

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.