Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to capitalize the first letter in dict values only

Here is a dictionary

dictionary = {'Happy':['SMILE','LAUGH','BEAMING'],
              'Sad':['FROWN','CRY','TEARS'],
              'Indifferent':['NEUTRAL','BLAND', 'MEH']}

I am trying to change the values in the entire dictionary such that SMILE becomes Smile, LAUGH becomes Laugh etc.

This is what I’m attempted

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{str(k).upper():str(v).upper() for k,v in dictionary.values()}

But the result of this is capitalizing the keys and does noting to the values.

>Solution :

Try the below

d = {'Happy':['SMILE','LAUGH','BEAMING'],
              'Sad':['FROWN','CRY','TEARS'],
              'Indifferent':['NEUTRAL','BLAND', 'MEH']}
d = {k:[x.capitalize() for x in v] for k,v in d.items()}
print(d)

output

{'Happy': ['Smile', 'Laugh', 'Beaming'], 'Sad': ['Frown', 'Cry', 'Tears'], 'Indifferent': ['Neutral', 'Bland', 'Meh']}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading