[Solved] how to export selected dictionary data into a file in python?


First, you start your question with expenses but ends with incomes and the code also has incomes in the parameters so I’ll go with the income.

Second, the error says the answer. “expense_types(income_types)” is a list and “expenses(incomes)” is a dictionary. You’re trying to find a list (not hashable) in a dictionary.

So to make your code work:

def export_incomes(incomes, income_types, file):
    items_to_export = []

    for u, p in incomes.items():
        if u in income_types:
            items_to_export.append(': '.join([u,p]) + '\n')  # whitespace after ':' for spacing
    
    with open(file, 'w') as f:
        f.writelines(items_to_export)

If I made any wrong assumption or got your intention wrong, pls let me know.

solved how to export selected dictionary data into a file in python?