0

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.

Astudent
  • 186
  • 1
  • 2
  • 16
  • It's probably easier to just replace elements instead of thinking of "editing" them. (Strings _must_ be done this way.) Try looping over your dictionary and replacing the values with a list comprehension that lowercases everything in the list? – Two-Bit Alchemist Nov 14 '18 at 16:45
  • (This assumes your data structure is pretty uniformly like what you showed. You need to add to your requirements if you have "misbehaving" keys or values.) – Two-Bit Alchemist Nov 14 '18 at 16:46

1 Answers1

2
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']}
mportes
  • 1,589
  • 5
  • 13