0

Hello I am learning python and I am confused with the count for my python for loop.

I am implementing the function make_str_from_column: This creates a string from a list of lists, representing a column.

What I dont understand is that when I set the count to 0 I get an error message. It works when I set the count to -1? would be grateful for your feedback, many thanks.

Here is the error message

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
  File "/Users/<name>/Downloads/a3.py", line 77, in make_str_from_column
    new_element = board[count][column_index]
IndexError: list index out of range
>>> 

Here is the function

def make_str_from_column(board, column_index):
    """ (list of list of str, int) -> str

    Return the characters from the column of the board with index column_index
    as a single string.

    >>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
    'NS'
    >>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 2)
    'TO'
    >>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 3)
    'TB'
    >>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
    'AX'
    >>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B'], ['X', 'S', 'O', 'B']], 1)
    'NSS'
    """

    # intialise a count for the loop
    count = -1

    # create an empty list to save the indexed elemenets into
    new_list = []

    for i in board:
        count = count + 1
        new_element = board[count][column_index]
        new_list.append(new_element)
    string_new_list = ''.join(new_list)
    return string_new_list

Also is there a more pythonic way of writing this

0

3 Answers 3

5

You are already accessing each element of board with the loop:

for i in board:

i is bound to each element in the board sequence, you do not need to use a separate counter here. The following works just fine:

for i in board:
    new_element = i[column_index]
    new_list.append(new_element)

or just use a list comprehension:

new_list = [row[column_index] for row in board]

which means your function can be reduced to:

def make_str_from_column(board, column_index):
    return ''.join([row[column_index] for row in board])

Python lists are indexed starting at 0, and you increment count before indexing. If count starts at 0 that means you use board[1], then board[2], etc. but you end up with len(board) as the last index. There is no such index, the last index is at len(board) - 1 instead.

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

Comments

0

All you need is:

def make_str_from_column(board, column_index):
    return ''.join(row[column_index] for row in board)

Comments

0

1. The error is because you are incrementing count at the beginning of the body of your for loop. So eventually, if the initial value of count is 0, you are going to go one index out of bound. This happens because, when count actually becomes the last index you want, the index you are actually using is count+1, instead of count.

You can set the count to 0, but just increment count at the end of your for loop.

Like this:

for i in board:
    new_element = board[count][column_index]
    new_list.append(new_element)
    count = count +1

2. However, as stated in another answer, you don't even need to do board[count] because

for i in board 

already iterates over every single element value of 'board'.

3. One exception, though, is a dictionary. If board was a dictionary, 'for i in board' only iterates over all the keys of the dictionary, not the values. Therefore, you still have to do

new_element = board[i][column_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.