Skip to content

e13b1_namedtuple_records.py throws KeyErrorΒ #6

@tnakatani

Description

@tnakatani

The solution in e13b1_namedtuple_records.py throws an error: KeyError: 'last'

In the solution below:

def format_sort_records(list_of_tuples):
    output = []
    template = '{last:10} {first:10} {distance:5.2f}'
    for person in sorted(list_of_tuples, key=operator.attrgetter('last', 'first')):
        output.append(template.format(*(person._asdict())))
    return output

I believe you need two asterisks in output.append to make it work:

output.append(template.format(**(person._asdict())))

Reference: https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions