Replacing values in a list by matching with dictionary

I am trying to replace the values in a list if a match is found with dictionary key. But i am getting the same list as output


data = ['18. September 2021', '7. Juni 1946', 'Mai 2021, 19', '16.9.2021']

dictionary = {"Januar":"January","Februar":"February","März":"March","April":"April","Mai":"May","Juni":"June",

for key in dictionary.keys():
    for index,i in enumerate(data):
        value = i.upper().replace(key, dictionary[key])
        data[index] = value


Expected Output

['18. September 2021', '7. June 1946', 'May 2021, 19', '16.9.2021']


The main problem with your code is the upper(): You convert the date to uppercase, but then try to replace the non-uppercase month names. Remove the upper() entirely, or make the dates uppercase after the replacements.

However, you could also combine the dictionary keys to a regular expression and use re.sub with a callback function to replace them all at once:

>>> import re
>>> p = "|".join(dictionary)
>>> p
>>> [re.sub(p, lambda m: dictionary[], d) for d in data]
['18. September 2021', '7. June 1946', 'May 2021, 19', '16.9.2021']