Stuck on Python dictionary and loops

I have been teaching myself Python using Edx audit courses, and I have been doing well, but I am now stuck on a dictionary problem. In this "assignment" I am supposed to go through a list of names (both first and last names), then create a dictionary of using the first name. I was able to do that, and do a count of how many times the name appears. In this assignment, I am supposed to create a dictionary of the "first name" and then create a value of all of the same names. So, if there is my_list = 'Bob A', 'Bob B', 'Bob C' the dictionary should say: 'Bob': 'Bob A', 'Bob B', Bob C'

This is what I have so far, but I keep getting a type error saying I cannot append to an int. Other times I have gotten a Key Value Error 'David' I believe, but I have been unable to reproduce this.

def name_lists(name_list):
    name_dict = {}
    for name in name_list:
        split_name = name.split()
        first_name = split_name[0]
        if first_name in name_dict:
            name_dict[first_name].append(name)
        name_dict[first_name] = name_list
    return name_dict   

name_list = ["Tony Alpha", "Tony Bravo", "Dillon Charlie",
             "Dillon Delta", "Michael Kilo", "Andi Nut",
             "Andi Desi", "Andi Horase", "Andi Tango",
             "Andi ANdi", "Michelle Marie"]
print(name_lists(name_list))

Thank you for any help you can give me. It looks like it should be easy, and I am sure I am missing something very simple. I want to make sure I can do all of this before I pay for everything.

3 answers

  • answered 2020-01-14 02:01 Mark Meyer

    Your code almost works. You need to have an else to go with:

     if first_name in name_dict:
    

    for the case when the name has not been seen that creates an array with the name. This way the next time you see that name the value will be a list you can append to. Something like:

    def name_lists(name_list):
        name_dict = {}
        for name in name_list:
            split_name = name.split()
            first_name = split_name[0]
            if first_name in name_dict:
                name_dict[first_name].append(name)
            else:                              # first_name is not in the dict, so
                name_dict[first_name] = [name] # add the array to the dict 
                                               # next time you can append.
        return name_dict 
    

    Result:

    {'Tony': ['Tony Alpha', 'Tony Bravo'], 'Dillon': ['Dillon Charlie', 'Dillon Delta'], 'Michael': ['Michael Kilo'], 'Andi': ['Andi Nut', 'Andi Desi', 'Andi Horase', 'Andi Tango', 'Andi ANdi'], 'Michelle': ['Michelle Marie']}
    

    There are, of course, more succinct ways to do this, but doing the long way is a good exercise.

  • answered 2020-01-14 02:03 milos.ai

    name_dict = {}
    
    name_list = ["Tony Alpha", "Tony Bravo", "Dillon Charlie",
                 "Dillon Delta", "Michael Kilo", "Andi Nut",
                 "Andi Desi", "Andi Horase", "Andi Tango",
                 "Andi ANdi", "Michelle Marie"]
    
    for full_name in name_list:
    
        first_name,last_name = full_name.split(' ')
    
        if first_name in name_dict:
            # there is already something with this key/first_name in dict, append to list
            name_dict[first_name] = name_dict[first_name] + [full_name]
        else:
            # we see this key(first_name) first time, create key-value 
            name_dict[first_name] = [full_name]
    
    print(name_dict)
    

    will give you:

    {'Tony': ['Tony Alpha', 'Tony Bravo'], 'Dillon': ['Dillon Charlie', 'Dillon Delta'], 'Michael': ['Michael Kilo'], 'Andi': ['Andi Nut', 'Andi Desi', 'Andi Horase', 'Andi Tango', 'Andi ANdi'], 'Michelle': ['Michelle Marie']}
    

  • answered 2020-01-14 02:12 Josh

    Use defaultdict instead of dict with default value list. This way you don't need to check if firstname is already in the dictionary.

    def name_lists(name_list):
        name_dict = defaultdict(list)
        for name in name_list:
           split_name = name.split()
           first_name = split_name[0]
           name_dict[first_name].append(name)
    
        return dict(name_dict) # convert to dict