I am attempting to replace the first string in the following list using the subsequent dictionary that is mapping to a list of strings.
id_list = ['40000', '58962', '85496']
id_dict = {'10000': ['10001','10002','10003'], '40000': ['40001','40002','40003']}
Using a defined function such as this:
def find_replace_multi_ordered(string, dictionary):
# sort keys by length, in reverse order
for item in sorted(dictionary.keys(), key = len, reverse = True):
string = re.sub(item, dictionary[item], string)
return string
# Credit: http://pythoninthewyld.com/2018/03/12/dict-based-find-and-replace-deluxe/
In the following for loop:
for i in id_list:
if id_list[0][-4:] == '0000':
id_list.replace(find_replace_multi_ordered(i, id_dict))
else:
pass
print(id_list)
This works for string to string dictionary mapping but results in a TypeError for sting to list mapping.
Error:
TypeError: unhashable type: 'list'
The desired output would be the following:
id_list = [['40001','40002','40003'], '58962', '85496']
Thank you for any and all recommendations!
id_dict['4000']?