Two approaches to capitalize the first letter of each element in a list in Python:
(1) Using List Comprehension:
my_list = ['red color', 'blue color', 'green color'] capitalize_list = [i.capitalize() for i in my_list] print(capitalize_list)
As highlighted in yellow, the first letter of each element in the list was capitalized:
['Red color', 'Blue color', 'Green color']
Please check the following guide if you wish to apply a title case instead.
(2) Using For Loop:
my_list = ['red color', 'blue color', 'green color'] capitalize_list = [] for i in my_list: capitalize_list.append(i.capitalize()) print(capitalize_list)
The result:
['Red color', 'Blue color', 'Green color']
Alternatively, you may use the following generator to capitalize the first letter of each element in a list:
Enter List Name:
Enter List Values:
Enter New List Name:
For example:
