Python | Convert string list into multiple cases
Last Updated :
13 Apr, 2023
Sometimes, while working with Python strings, we might have a problem in which we have list of strings and we wish to convert them into specified cases. This problem generally occurs in the cases in which the strings we receive are ill-cased. Let's discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + inbuilt functions
In this method, we use list comprehension as a shortened way to perform this task rather than loop method that could span some lines of codes. The conversions are made using generic inbuilt functions that can perform interconversion tasks.
Python3
# Python3 code to demonstrate working of
# Convert string list into multiple cases
# Using inbuilt functions + list comprehension
# Initializing list
test_list = ['bLue', 'ReD', 'yeLLoW']
# printing original list
print("The original list is : " + str(test_list))
# Convert string list into multiple cases
# Using inbuilt functions + list comprehension
res = [(ele.upper(), ele.title(), ele.lower()) for ele in test_list]
# printing result
print("The list with multiple cases are : " + str(res))
Output : The original list is : ['bLue', 'ReD', 'yeLLoW']
The list with multiple cases are : [('BLUE', 'Blue', 'blue'), ('RED', 'Red', 'red'), ('YELLOW', 'Yellow', 'yellow')]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. list comprehension + inbuilt functions performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using map() + lambda + inbuilt functions
This is another method to perform this particular task. In this, we just perform the task of extending the logic of conversions using lambda and iterations, and application to each string is done by lambda function.
Python3
# Python3 code to demonstrate working of
# Convert string list into multiple cases
# Using map() + lambda + inbuilt functions
# Initializing list
test_list = ['bLue', 'ReD', 'yeLLoW']
# printing original list
print("The original list is : " + str(test_list))
# Convert string list into multiple cases
# Using map() + lambda + inbuilt functions
res = list(map(lambda ele: (ele.upper(), ele.title(), ele.lower()), test_list))
# printing result
print("The list with multiple cases are : " + str(res))
Output : The original list is : ['bLue', 'ReD', 'yeLLoW']
The list with multiple cases are : [('BLUE', 'Blue', 'blue'), ('RED', 'Red', 'red'), ('YELLOW', 'Yellow', 'yellow')]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using a loop and string methods
Uses a loop to iterate through the elements of the list, and for each element, it calls the upper(), title(), and lower() methods to convert the string to upper case, title case, and lower case, respectively. It then appends a tuple of the three converted strings to the res list. Finally, it prints the res list.
Python3
# Python3 code to demonstrate working of
# Convert string list into multiple cases
# Using a loop and string methods
# Initializing list
test_list = ['bLue', 'ReD', 'yeLLoW']
# printing original list
print("The original list is : " + str(test_list))
# Convert string list into multiple cases
# Using a loop and string methods
res = []
for ele in test_list:
res.append((ele.upper(), ele.title(), ele.lower()))
# printing result
print("The list with multiple cases are : " + str(res))
OutputThe original list is : ['bLue', 'ReD', 'yeLLoW']
The list with multiple cases are : [('BLUE', 'Blue', 'blue'), ('RED', 'Red', 'red'), ('YELLOW', 'Yellow', 'yellow')]
Time Complexity: O(nm), where n is the number of elements in the input list and m is the maximum length of any string in the input list.
Auxiliary Space: O(n), where n is the number of elements in the input list.
Similar Reads
Python | Convert string List to Nested Character List Sometimes, while working with Python, we can have a problem in which we need to perform interconversion of data. In this article we discuss converting String list to Nested Character list split by comma. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehen
7 min read
Python | Convert string enclosed list to list Given a list enclosed within a string (or quotes), write a Python program to convert the given string to list type. Examples: Input : "[0, 2, 9, 4, 8]" Output : [0, 2, 9, 4, 8] Input : "['x', 'y', 'z']" Output : ['x', 'y', 'z'] Approach #1: Python eval() The eval() method parses the expression passe
5 min read
Python - Convert case of elements in a list of strings In Python, we often need to convert the case of elements in a list of strings for various reasons such as standardizing input or formatting text. Whether it's converting all characters to uppercase, lowercase, or even swapping cases. In this article, we'll explore several methods to convert the case
3 min read
Python - Convert List of Integers to a List of Strings We are given a list of integers and our task is to convert each integer into its string representation. For example, if we have a list like [1, 2, 3] then the output should be ['1', '2', '3']. In Python, there are multiple ways to do this efficiently, some of them are: using functions like map(), re
3 min read
Create a List of Strings in Python Creating a list of strings in Python is easy and helps in managing collections of text. For example, if we have names of people in a group, we can store them in a list. We can create a list of strings by using Square Brackets [] . We just need to type the strings inside the brackets and separate the
3 min read
Case-insensitive string comparison in Python The goal here is to compare strings in a list without considering their case, meaning the comparison should ignore whether letters are uppercase or lowercase. For example, given the list ["hello", "HELLO", "HeLLo"], we want to check if all strings are equal, ignoring the case. Let's explore differen
2 min read