Here are 3 ways to apply a title case to each element in a Python list (special case for apostrophes is included):
(1) Using a List Comprehension:
my_list = ["red color", "blue color", "green color"]
title_list = [i.title() for i in my_list]
print(title_list)
As highlighted in yellow, a title case was applied to each element in the list:
['Red Color', 'Blue Color', 'Green Color']
(2) Using a For Loop:
my_list = ["red color", "blue color", "green color"] title_list = [] for i in my_list: title_list.append(i.title()) print(title_list)
Here are the same results:
['Red Color', 'Blue Color', 'Green Color']
(3) Special case for apostrophes:
import string
my_list = ["red's color", "blue's color", "green's color"]
title_list = [string.capwords(i) for i in my_list]
print(title_list)
The result:
["Red's Color", "Blue's Color", "Green's Color"]
Additional resources: