How to make the dictionary return a list value?

Question:

Write a get_phone_numbers_for_countries function that will:

Receive a list of phone numbers.
Sanitize (normalize) the received customer phone list using our sanitize_phone_number function.
Sort phone numbers by the countries indicated in the table.
Return a dictionary with lists of phone numbers for each country as follows:

{
    "UA": [<phone list here>],
    "JP": [<phone list here>],
    "TW": [<phone list here>],
    "SG": [<phone list here>]
}

If it was not possible to match the phone code with known ones, this phone should be added to the dictionary list with the key ‘UA’.

My code:

def sanitize_phone_number(phone):
    new_phone = (
        phone.strip()
        .removeprefix("+")
        .replace("(", "")
        .replace(")", "")
        .replace("-", "")
        .replace(" ", "")
    )
    return new_phone


def get_phone_numbers_for_countries(list_phones):
    dct = dict()
    for num in list_phones:
        num = sanitize_phone_number(num)
        if num[:3] == '380':
            dct.update({"UA": num})
        elif num[:2] == '81':
            dct.update({"JP": num})
        elif num[:2] == '65':
            dct.update({"SG": num})
        elif num[:3] == '886':
            dct.update({"TW": num})
        else:
            dct.update({"UA": num})
            
    return dct
 

The function returns an invalid value: {‘UA’: ‘380998759405’, ‘JP’: ‘818765347’, ‘TW’: ‘8867658976’, ‘SG’: ‘657658976’}. Should be: test_get_phone_numbers_for_countries([‘380998759405’, ‘818765347’, ‘8867658976’, ‘657658976’]) == {‘UA’: [‘380998759405’], ‘JP’: [‘818765347’], ‘TW’: [‘8867658976’], ‘SG’: [‘657658976’]}

Help fix the code

Asked By: Alduin

||

Answers:

You are using .update which will replace the previous value with your new one. Instead you could use dct["UA"] += [num]

Also if your else statement adds the number to UA then you don’t need the first condition of your if statement

Answered By: SophieOH

This is because dict.update replaces the values of the dictionary with the values of the dictionary passed as parameters.

To add them I could advise you :

def sanitize_phone_number(phone):
    new_phone = (
        phone.strip()
        .removeprefix("+")
        .replace("(", "")
        .replace(")", "")
        .replace("-", "")
        .replace(" ", "")
    )
    return new_phone


def append_number(dct, country, num):
    if country not in dct:
        dct[country] = []

    dct[country].append(num)


def get_phone_numbers_for_countries(list_phones):
    dct = dict()
    for num in list_phones:
        num = sanitize_phone_number(num)
        if num[:3] == "380":
            append_number(dct, "UA", num)
        elif num[:2] == "81":
            append_number(dct, "JP", num)
        elif num[:2] == "65":
            append_number(dct, "SG", num)
        elif num[:3] == "886":
            append_number(dct, "TW", num)
        else:
            append_number(dct, "UA", num)

    return dct

Et avec get_phone_numbers_for_countries(["380998759405", "818765347", "8867658976", "657658976"]) j’obtiens : {'UA': ['380998759405'], 'JP': ['818765347'], 'TW': ['8867658976'], 'SG': ['657658976']}

Answered By: Jay
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.