Search code examples
python-3.xlistdictionarylowercase

Editing string in a list inside a dictionary in python3


If I have a dictionary like this:

{'alfa': ['Computer Science'], 'beta': ['book', 'CompUter']}

And I want to turn it into a dictionary like this:

{'alfa': ['computer science'], 'beta': ['book', 'computer']}

So basically turn the words into lowercase letters. for this i know I would need the function lower().

However, I do not know how to access the words inside the dictionary, so that I could use this function.

Before putting the list into the dictionary, I tried this:

for z in wordlist:
    z.lower()

But it didn't do anything to the words.


Solution

  • my_dict = {'alfa': ['Computer Science'], 'beta': ['book', 'CompUter']}
    
    for key in my_dict:
        my_dict[key] = [my_str.lower() for my_str in my_dict[key]]
    
    print(my_dict)
    

    Output:

    {'alfa': ['computer science'], 'beta': ['book', 'computer']}