'having a hard time with columns in python

I am having such a hard time understanding how to make a column-base data table in python and inside a Jupyter notebook. I have attached what my output looks like and the instructions on how to implement them. I'm so close yet so far. Here is my code:

def head(column_table: dict[str, list[str]], rows: int) -> dict[str, list[str]]:
    """Produce a column-based table with only the first N rows of data for each column."""
    result: dict[str, list[str]] = {}
    for column in column_table:
        new_list: list[str] = []
        for column in column_table:
            new_list.append(column)
    result[column] = new_list
    return result

Please help! What am I doing wrong?

Instructions on what I need to do.

What my output looks like.



Solution 1:[1]

I don't usually like to give answers to homework question, but I don't think you're on the right track here.

def head(column_table: dict[str, list[str]], rows: int) -> dict[str, list[str]]:
    """Produce a column-based table with only the first N rows of data for each column."""
    result: dict[str, list[str]] = {}
    for key,data in column_table.items():
        result[key] = data[:rows]
    return result

Or, if you insist on using a loop:

def head(column_table: dict[str, list[str]], rows: int) -> dict[str, list[str]]:
    """Produce a column-based table with only the first N rows of data for each column."""
    result: dict[str, list[str]] = {}
    for key,data in column_table.items():
        newrow = []
        for i in range(rows):
            newrow[i] = data[i]
        result[key] = newrow
    return result

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Roberts