Skip to content
Advertisement

how to add file names into dictionary based on their prefix?

I have a following problem. I have a list containing file names:

list_files = ["12_abc.txt", "12_ddd_xxx.pdf", "23_sss.xml", "23_adc.txt", "23_axx_yyy.pdf"]

I need to add them into dictionary based on their prefix number, i.e. 12 and 23. Each value of the dictionary should be a list containing all files with the same prefix. Desired output is:

dictionary = {"12": ["12_abc.txt", "12_ddd_xxx.pdf"], "23": ["23_sss.xml", "23_adc.txt", "23_axx_yyy.pdf"]}

What I tried so far:

dictionary = {}
    for elem in list_files:
        prefix = elem.split("_")[0]
        dictionary[prefix] = elem

However this gives me the result {'12': '12_ddd_xxx.pdf', '23': '23_axx_yyy.pdf'}. How can I add my elem into a list within the loop, please?

Advertisement

Answer

Try:

list_files = ["12_abc.txt", "12_ddd_xxx.pdf", "23_sss.xml", "23_adc.txt", "23_axx_yyy.pdf"]

dictionary = {}
for f in list_files:
    prefix = f.split('_')[0]  # or prefix, _ = f.split('_', maxsplit=1)
    dictionary.setdefault(prefix, []).append(f)

print(dictionary)

Prints:

{'12': ['12_abc.txt', '12_ddd_xxx.pdf'], '23': ['23_sss.xml', '23_adc.txt', '23_axx_yyy.pdf']}

EDIT: Added maxsplit=1 variant, thanks @Ma0

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement