vojtam
vojtam

Reputation: 1225

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?

Upvotes: 1

Views: 83

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195418

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

Upvotes: 4

Christian Sloper
Christian Sloper

Reputation: 7510

This is a good place to use defaultdict from collections.

from collections import defaultdict

d = defaultdict(list)
list_files = ["12_abc.txt", "12_ddd_xxx.pdf", "23_sss.xml", "23_adc.txt", "23_axx_yyy.pdf"]

for elem in list_files:
    prefix =  elem.split("_")[0]
    d[prefix].append(elem)

yields:

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

Upvotes: 2

Related Questions