Python Forum
how to capitalize letter in list object Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to capitalize letter in list object Python
#1
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.
Reply
#2
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']
snippsat and Pedroski55 like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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)
Reply
#4
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
Reply
#5
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()
}
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  add object and name in list 3lnyn0 4 1,449 Nov-24-2022, 07:33 PM
Last Post: buran
  python update binary object (override delivered Object properties) pierre38 4 1,982 May-19-2022, 07:52 AM
Last Post: pierre38
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 5,368 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,633 Jan-19-2022, 08:33 AM
Last Post: menator01
  AttributeError class object has no attribute list object scttfnch 5 3,755 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  Trying to get the first letter of every word in a list DanielCook 2 2,286 Jan-05-2021, 05:06 PM
Last Post: deanhystad
  'list' object not callable sidra 5 5,906 Nov-29-2020, 04:56 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,323 Nov-13-2020, 04:59 AM
Last Post: perfringo
  How to capitalize in dictionary Inkanus 2 3,906 Oct-28-2020, 01:20 PM
Last Post: Inkanus
  TypeError: 'list' object is not callable Python_Mey 1 2,621 Aug-25-2020, 03:56 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020