0

I want to use a for loop to find a list, within a list of lists.

Why does it return list 0 instead of list 2?

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([['H', 'O', 'U', 'S', 'E'], ['B', 'E', 'D'], ['C', 'H', 'E', 'E', 'S', 'E']], 2)
    'CHEESE'
    """

    letter = ''
    line = ''

    for row_index in board:
        for letter in row_index:
            line = line + letter
        return line

make_str_from_row([['H', 'O', 'U', 'S', 'E'], ['B', 'E', 'D'], ['C', 'H', 'E', 'E', 'S', 'E']], 2)
2
  • 1
    Get rid of your outer loop. Do for letter in board[row_index]: for your inner loop. Or get rid of all loops and just return ''.join(board[row_index]). Commented Feb 17, 2017 at 3:10
  • Thanks @StevenRumbalski Very clear, concise explaination! Commented Feb 17, 2017 at 3:28

2 Answers 2

2

row_index is not what you think it is. In Python, the for loop syntax you're using causes the control variable to store the element in a collection (in this context). In other words, you're iterating through each sub-list in board, where for each iteration,row_index is updated to point to the next sub-list. What you need is a single access and then your inner for loop:

sub_list = board[row_index]
    for letter in sub_list:
        line = line + letter
    return line

The following is equivalent:

    for letter in board[row_index]:
        line = line + letter
    return line

On another note, since your function is meant to return a concatenated String version of a given sub_list, you may consider the one-line solution...

return ''.join(board[row_index])

...to concatenate the characters in the string at board[row_index]. join is considered more efficient than += because it avoids the wasteful constructing and discarding of sub-strings. (You create a completely different String for each +=.) Especially for long Strings, join is a good solution.

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

3 Comments

Thanks @synchronizer! I'm a little confused about one thing. Why does the function return the sub-list[0]. If the function is iterating though every sub-list, why does it not return all the sub-lists? Thanks!
@sim You're welcome. To answer your question, the function retruns only sub-list 0 as a String because you return pre-maturely within your loop. That is, you return at the end of the first iteration before the loop has a chance to append the characters from the rest of the sub_lists. (That's the undesired behavior though). Does that help?
That's awesome! Headache removed! :D
0

The problem is that you are always returning the first element of the list. Keep in mind that return makes the function to immediately finish. In addition, your variable letter could be removed. You do not need to initialize it since it is part of your for loop. Finally, your code could be simply:

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([['H', 'O', 'U', 'S', 'E'], ['B', 'E', 'D'], ['C', 'H', 'E', 'E', 'S', 'E']], 2)
    'CHEESE'
    """

    return ''.join(board[row_index])

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.