Posts: 1
Threads: 1
Joined: May 2024
May-29-2024, 10:17 AM
(This post was last modified: May-29-2024, 11:46 AM by Larz60+.)
Could you please kindly help me how to capitalize list object from the dictionary ?
shopping_dict = {
'bakery':['bread', 'doughnut', 'rolls'],
'greengrocer':['carrot', 'celery, 'arugula'],
}
print("Shopping list:")
for k, v in shopping_dict.items():
print(f"I'm going to, {k.capitalize()}, I'm buying here:{v}") the output should be :
Output: Shopping list:
I'm going to, Bakery, I'm buying here:['Bread', 'Doughnut', 'Rolls'],
I'm going to, Greengrocer, I'm buying here:['Carrot', 'Celery, 'Arugula']
Thank you for your help
Larz60+ write May-29-2024, 11:46 AM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time, also fixed indentation. Please use BBCode tags on future posts.
Posts: 1,145
Threads: 114
Joined: Sep 2019
Use pythons string title()
shopping_dict = {
'bakery':['bread', 'doughnut', 'rolls'],
'greengrocer':['carrot', 'celery', 'arugula'],
}
print("Shopping list:")
for k, v in shopping_dict.items():
v = [v.title() for v in v]
print(f"I'm going to, {k.title()}, I'm buying here:{v}") Output
Output: Shopping list:
I'm going to, Bakery, I'm buying here:['Bread', 'Doughnut', 'Rolls']
I'm going to, Greengrocer, I'm buying here:['Carrot', 'Celery', 'Arugula']
Pedroski55 and snippsat like this post
Posts: 1,094
Threads: 143
Joined: Jul 2017
Do it yourself:
shopping_dict = {
'bakery':['bread', 'doughnut', 'rolls', 'carrotcake', 'biscuits'],
'greengrocer':['carrot', 'celery', 'arugula', 'chard', 'champignons'],
'wine shop': ['rioja', 'chardonnay', 'tempranillo', 'carménere']
}
# make the first letter of a word a capital letter
def start_big(word):
item = word[0].upper() + word[1:]
return item
for key in shopping_dict.keys():
shopping_dict[key] = [start_big(shopping_dict[key][i]) for i in range(len(shopping_dict[key]))]
for item in shopping_dict.items():
print(item)
Posts: 7,320
Threads: 123
Joined: Sep 2016
May-29-2024, 03:52 PM
(This post was last modified: May-29-2024, 03:52 PM by snippsat.)
Pedroski55 as info range(len(sequence)) is to be avoid as is a bad way in most cases.
Also there no need for it your code.
shopping_dict[key] = [start_big(shopping_dict[key][i]) for i in range(len(shopping_dict[key]))]
# Better
shopping_dict[key] = [start_big(item) for item in shopping_dict[key]] menator01 code is fine,with a helper function can be like this.
shopping_dict = {
'bakery': ['bread', 'doughnut', 'rolls', 'carrotcake', 'biscuits'],
'greengrocer': ['carrot', 'celery', 'arugula', 'chard', 'champignons'],
'wine shop': ['rioja', 'chardonnay', 'tempranillo', 'carménere']
}
def cap_start(word):
return word.title()
for key in shopping_dict:
shopping_dict[key] = [cap_start(item) for item in shopping_dict[key]]
for item in shopping_dict.items():
print(item)
Pedroski55 likes this post
Posts: 2,126
Threads: 11
Joined: May 2017
A nested dict-list comprehension could do this:
shopping_dict = {
'bakery': ['bread', 'doughnut', 'rolls', 'carrotcake', 'biscuits'],
'greengrocer': ['carrot', 'celery', 'arugula', 'chard', 'champignons'],
'wine shop': ['rioja', 'chardonnay', 'tempranillo', 'carménere']
}
shopping_dict_title = {key: [word.title() for word in words] for key, words in shopping_dict.items()} For better understanding, you could use more than one line:
shopping_dict_title = {
key: [word.title() for word in words]
for key, words in shopping_dict.items()
} Another approach is the use of list , map , str.title in a dict comprehension:
shopping_dict_title = {
key: list(map(str.title, words))
for key, words in shopping_dict.items()
}
|