Ad

Define a function that accepts a list of names, and returns a list of lists. Eacb name in the input list will get grouped into a smaller list of names that start with the same letter, and all those small groups are returned as a list of lists.

Each list within the list should be sorted alphabetically, and the outer list should be sorted by whatever the starting letter of the contained words is.

Example:
["joe","larry","jim","foobar", "lisa","frank"]

becomes:

[
["foobar","frank"],
["jim", "joe"],
["larry", "lisa"]
]

def bucketize(names):
    # name_dict will group names by starting letter into a dict,
    # where the dict key will be the letter, and value will be a list of 
    # names that start with that letter.  like this..
    # {'j':['jim','joe'], 'f':['frank','finnegan']}
    name_dict = {}
    for name in names:
        first_letter = name[0]
        # we don't know if this is this bucket exists yet 
        if first_letter in name_dict:
            name_dict[first_letter].append(name)
        else:
            name_dict[first_letter] = [name]
    # need to convert the dict into just a list of lists
    big_list = []
    for key in name_dict:
        bucket = name_dict[key]
        sorted_bucket = sorted(bucket)
        big_list.append(sorted_bucket)
    # we have a list of sorted buckets, but need to sort the list
    return sorted(big_list)