avatar Bite 180. Group names by country

In this Bite you are presented with a list of surnames, names, and countries. These 3 fields are in a multiline string, separated by a comma.

Code group_names_by_country, looping through the list, returning a collections.defaultdict where the keys are countries and the values are lists of concatenated names and surnames. The order of the keys (countries) and the value lists (names) does not matter.

Here is an example of a possible input to your function:

data = """last_name,first_name,country_code
Watsham,Husain,ID
Harrold,Alphonso,BR
Apdell,Margo,CN
Tomblings,Deerdre,RU
Wasielewski,Sula,ID
Jeffry,Rudolph,TD
Brenston,Luke,SE
Parrett,Ines,CN
Braunle,Kermit,PL
Halbard,Davie,CN"""

... and the output your function should return:

defaultdict(<class 'list'>,
            {'BR': ['Alphonso Harrold'],
             'CN': ['Margo Apdell', 'Ines Parrett', 'Davie Halbard'],
             'ID': ['Husain Watsham', 'Sula Wasielewski'],
             'PL': ['Kermit Braunle'],
             'RU': ['Deerdre Tomblings'],
             'SE': ['Luke Brenston'],
             'TD': ['Rudolph Jeffry']})

Good luck and keep calm and code in Python! Read up on collections, one of our favorite standard library modules!

Login and get coding
go back Beginner level
Bitecoin 2X

576 out of 580 users completed this Bite.
Will you be the 577th person to crack this Bite?
Resolution time: ~36 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 4.23 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help