Python - Convert case of elements in a list of strings
Last Updated :
30 Dec, 2024
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 of elements in a list of strings.
Using List Comprehension
List comprehension is one of the most efficient ways to process each element in a list. It allows us to create a new list by applying a function (such as lower()
or upper()
) to each string in the original list in a concise and readable manner.
Python
s1 = ['Geeks', 'for', 'Geeks']
#lower strings
s2 = [s.lower() for s in s1]
print(s2)
Output['geeks', 'for', 'geeks']
Explanation:
- We use list comprehension to iterate over each element s in the list
s1
. - The method
lower()
is applied to each string to convert it to lowercase. - This method is fast and directly applies the transformation to each string.
Let's explore some more methods and see how we can convert the case of elements in a list of strings.
Using map()
map()
function is another efficient method for applying a function to every element in an iterable. It returns an iterator, and when combined with list()
, we can create a new list after converting the case of the strings.
Python
s1 = ['Geeks', 'for', 'Geeks']
s2 = list(map(str.lower, s1))
print(s2)
Output['geeks', 'for', 'geeks']
Explanation:
- The
map()
function applies str.lower
to each string in the list s1
. - The result is then converted to a list using
list()
. - This method is similar to list comprehension but uses an iterator, making it a bit more memory-efficient for large lists.
Using For Loop
Using a simple for
loop is another approach, though it's a bit slower because we manually iterate and apply the function to each element.
Python
s1 = ['Python', 'is', 'fun!']
s2 = []
for s in s1:
s2.append(s.lower())
print(s1)
Output['Python', 'is', 'fun!']
Explanation:
- We initialize an empty list
s2
. - Using a
for
loop, we iterate through each string s
in the s1
list. - The method
lower()
is applied to each string, and the result is appended to the s2
list.
Using append()
(Manual Method)
This method is similar to the previous one but includes a step-by-step breakdown of how we manually append transformed strings into a new list. It's the least efficient because of the overhead of repeatedly calling append()
and the explicit loop.
Python
s1 = ['Python', 'is', 'fun!']
s2 = []
for s in s1:
s2.append(s.upper())
print(s1)
Output['Python', 'is', 'fun!']
Explanation:
- We manually iterate over each string in the list
s1
. - The
upper()
method is used to convert each string to uppercase. - Each converted string is appended to the
s2
list.
Similar Reads
Python program to Convert a elements in a list of Tuples to Float Given a Tuple list, convert all possible convertible elements to float. Input : test_list = [("3", "Gfg"), ("1", "26.45")] Output : [(3.0, 'Gfg'), (1.0, 26.45)] Explanation : Numerical strings converted to floats. Input : test_list = [("3", "Gfg")] Output : [(3.0, 'Gfg')] Explanation : Numerical str
5 min read
Convert list of strings to list of tuples in Python Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read
How To Convert Comma-Delimited String to a List In Python? In Python, converting a comma-separated string to a list can be done by using various methods. In this article, we will check various methods to convert a comma-delimited string to a list in Python.Using str.split()The most straightforward and efficient way to convert a comma-delimited string to a l
1 min read
Python - Convert list of strings and characters to list of characters Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Using itertools.chain()itertools.chain() combines multiple lists or iterables i
3 min read
Convert List of Tuples to List of Strings - Python The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings.For example, giv
3 min read