Home > Back-end >  Append a list inside of a Dictionary
Append a list inside of a Dictionary

Time:10-09

I have a script to track donations below. My add_name() function is working up until I try to add the donation amount to the end of the list for an existing name. If the name already exists it overwrites the list associated with the name. I have tried append and extend, but neither work.

people_db = {'Bob': [600.0],
             'Fred': [645.0, 231.0, 768.0],
             'Sam': [4200.0, 5002.0, 100.0, 4302.0],
             'Faith': [742.0, 842.0, 542.0],
             'John': [432.0, 754.0, 10000]}

def add_name():
    name_person = str.lower(input("Enter Full Name or 'list': "))
    if name_person == "list":
        for key in people_db.keys():
            print(key)
    else:
        people_db[name_person] = list()
        people_db[name_person].extend([int(input("Donation Amount: "))])

Thanks for any help!

CodePudding user response:

You have two conditions (one when the user exists and one when not) so an if seems in order. Get the donation once then check the db for the name to see which way it should be updated.

people_db = {'Bob': [600.0],
             'Fred': [645.0, 231.0, 768.0],
             'Sam': [4200.0, 5002.0, 100.0, 4302.0],
             'Faith': [742.0, 842.0, 542.0],
             'John': [432.0, 754.0, 10000]}

def add_name():
    name_person = str.lower(input("Enter Full Name or 'list': "))
    if name_person == "list":
        for key in people_db.keys():
            print(key)
    else:
        donation = int(input("Donation Amount: "))
        if name_person in people_db:
            people_db[name_person].append(donation)
        else:
            people_db[name_person] = [donation]

You could use collections.defaultdict to do the if for you, but since your db is using regular dict already, its not worth the change.

CodePudding user response:


people_db = {'Bob': [600.0],
             'Fred': [645.0, 231.0, 768.0],
             'Sam': [4200.0, 5002.0, 100.0, 4302.0],
             'Faith': [742.0, 842.0, 542.0],
             'John': [432.0, 754.0, 10000]}

def add_name():
    name_person = str.lower(input("Enter Full Name or 'list': "))
    if name_person == "list":
        for key in people_db.keys():
            print(key)
    else:
        people_db[name_person] = list()
        people_db[name_person].append(int(input("Donation Amount: ")))
    for key, value in people_db.items():
        print(f'{key=} => {value=}')


def print_hi():
    add_name()


if __name__ == '__main__':
    print_hi()

Output:

Enter Full Name or 'list': shmr
Donation Amount: 123
key='Bob' => value=[600.0]
key='Fred' => value=[645.0, 231.0, 768.0]
key='Sam' => value=[4200.0, 5002.0, 100.0, 4302.0]
key='Faith' => value=[742.0, 842.0, 542.0]
key='John' => value=[432.0, 754.0, 10000]
key='shmr' => value=[123]

CodePudding user response:

This is a good use case of python's defaultdict from the collections library.

people_db = {'Bob': [600.0],
             'Fred': [645.0, 231.0, 768.0],
             'Sam': [4200.0, 5002.0, 100.0, 4302.0],
             'Faith': [742.0, 842.0, 542.0],
             'John': [432.0, 754.0, 10000]}
from collections import defaultdict
dd = defaultdict(list)
for person, donation_list in people_db.items():
    dd[person] = donation_list

dd['John'].append(5)
dd['Jane'].append(10)

print(dd)

Output

defaultdict(list,
            {'Bob': [600.0],
             'Fred': [645.0, 231.0, 768.0],
             'Sam': [4200.0, 5002.0, 100.0, 4302.0],
             'Faith': [742.0, 842.0, 542.0],
             'John': [432.0, 754.0, 10000, 5],
             'Jane': [10]})
  • Related