replace dictionary values with a list of values

You can make a new dict with the same keys as dict_1. and every value is a list containing all file names that start with the key.

dict_1 = {'ABC1': None, 'BBC2': None, 'PPP13': None, '1FGEE':None, 'STUFF':None, 'LUB23':None, 'UNIT44':None, 'ZX3454F2':None, 'AMS76':None, 'LLPT43':None}
list_info = ['ABC1-99.txt', 'BBC2-qp.txt', 'PPP13-jj5.txt', 'PPP13-frr.txt', '1FGEE-oop2.txt', 'STUFF-34534.txt', 'LUB23-j873.txt', 'UNIT44-oi5.txt', 'ZX3454F2-k.txt', 'UNIT44-de3t.txt', 'AMS76-light.txt', 'LLPT43-ifg.txt']

new_dict = {k: [fname for fname in list_info if fname.startswith(k)] for k in dict_1}

# if you need to modify dict_1 in place
{'ABC1': ['ABC1-99.txt'], 'BBC2': ['BBC2-qp.txt'], 'PPP13': ['PPP13-jj5.txt', 'PPP13-frr.txt'], '1FGEE': ['1FGEE-oop2.txt'], 'STUFF': ['STUFF-34534.txt'], 'LUB23': ['LUB23-j873.txt'], 'UNIT44': ['UNIT44-oi5.txt', 'UNIT44-de3t.txt'], 'ZX3454F2': ['ZX3454F2-k.txt'], 'AMS76': ['AMS76-light.txt'], 'LLPT43': ['LLPT43-ifg.txt']}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top