1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Rep Power

    Help with Lists within Lists

    Yes, this is for a class -- I am trying to return part of a nested list and am getting an error I don't understand. Here is what I have so far

    def make_str_from_row(board, row_index):
    ''' (list of list of str, int) -> str

    Return the characters from the row of the board with index row_index
    as a single string.

    >>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)

    result = ''
    for b in board(row_index):
    result = ('').join(b)

    What it is supposed to do (as is stated in the comments area) is take the first list ['A', 'N', 'T', 'T'] and print out 'ANTT'

    I am getting the error:

    Traceback (most recent call last):
    File "<pyshell#13>", line 1, in <module>
    make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
    File "C:\Users\Beverly\Documents\Melinda\intro_to_programming\a3.py", line 47, in make_str_from_row
    for b in board(row_index):
    TypeError: 'list' object is not callable

    I'm pretty sure where this nested list contains strings that I have to join the 4 indexes -- I'm not sure what I'm doing right or wrong at this point! Thanks for any advice
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power

    Python uses square bracket syntax, ie: board[row_index] to extract an item from a sequence. Name(parentheses) is a function call, and that's the immediate problem.

    Your program still won't work. Run it in the debugger watching the values and types of the variables as you step through, or liberally place print statements in your code. I've found that diagnostics are most useful to me when they include a comment to reference the statement, the data type, the value, if an object also str(dir(obj)) and help(obj).

    Parentheses indicate a generator, tuple, or function arguments. OOPS! Too much information.

    is always a function call whether the thing in parentheses specifies a generator or arguments.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo