| by Arround The Web | No comments

Python Lowercase Elements in a List

Python language allows the developer to perform various types of conversions. For instance, transforming a list of values to lowercase. The conversion to lowercase allows us to perform various tasks with the words/strings such as finding them, sorting them, or updating them. This Python blog will teach you various methods that are used to convert the list elements into lowercase.

How to Convert List Elements into Lower Case in Python?

Python uses the following methods to convert the list elements into lowercase:

Method 1: Convert List Elements into Lower Case Elements Using the “string.lower()” Method

In Python, the “string.lower()” method is applied to convert all the capital letters or Uppercase in a string to lowercase letters and construct a new string.

Syntax

string.lower()

 

This method does not take any parameters. Also, it ignores symbols and numbers.

Example

The Python code given below is used to retrieve the lowercase element in a list:

list_player = ["MESSI","Ronaldo","neYMAr"]
for item in range(len(list_player)):
    list_player[item] = list_player[item].lower()
print(list_player)

 

In the above code, the “for” loop iterates over the list elements/items and transforms each element of the list into lowercase using the “lower()” method.

Output

This output implies that all the list elements have been converted into lowercase.

Method 2: Convert List Elements into Lower Case Elements Using the “map()” Function

In Python, the “map()” function executes a function to a specific element in an iterable, such as str, list, or tuple, and constructs a new iterable with the modified elements. This method is used along with other built-in functions to convert the list string elements into lowercase.

Syntax

map(function, iterable)

 

In the above syntax, “function” specifies the function to apply to each element, and “iterable” specifies the iterable.

Example

The following code snippet uses the “map()” function along with the “lambda” and “list()” functions to represent the list elements in lowercase:

list_player = ["MESSI","Ronaldo","neYMAr"]
map_object = map(lambda x: x.lower(), list_player)
lc_list = list(map_object)
print(lc_list)

 

In the above code block, the “map()” function takes the “lambda” function and the given “list” as its arguments, respectively. The “lambda” function applies the “lower()” method to each element of the given list and converts the list elements into lowercase. Lastly, the mapped object is converted into the list using the “list()” function.

Output

The output indicates that the list elements have been converted into lowercase appropriately.

Method 3: Convert List Elements into Lower Case Elements Using the “List Comprehension” Technique

The “List Comprehension” method also works with the “lower()” method and “for” loop to convert the list string elements into lowercase.

Syntax

newlist = [expression for element/item in iterable if condition]

 

Example

This block is utilized to convert the elements of the list to lowercase:

list_player = ["MESSI","Ronaldo","neYMAr"]
new_list = [x.lower() for x in list_player]
print(new_list)

 

Here, the “List Comprehension” approach is used with the “for” loop to iterate along the elements of the list and convert each string element of the list into lowercase.

Output

Method 4: Convert List Elements into Lower Case Elements Using “for” Loop and “append()” Method

The “for” loop is used to iterate over the sequence such as list, string, tuple, etc., and the “append()” method is used to append the elements to the end of the list. These approaches can also be applied to apply the desired functionality.

Syntax

list.append(item)

 

Example

The program below is utilized to convert the Python list elements into lowercase:

list_player = ["MESSI","ronaldo","neYMAr"]
new_list = []
for x in list_player:
    new_list.append(x.lower())
print(new_list)

 

In this example code, the “list” containing string elements and an “empty list” is initialized, respectively. After that, the “for” loop is used to iterate through the list and use the “append()” and “lower()” methods to convert each element of the list into lowercase.

Output

This output displays that the lowercase elements have been retrieved from the given list accordingly.

Conclusion

To convert list elements into lowercase elements in Python, the “string.lower()” method, the “map()” function, “for” loop with “append()” method, or the “List Comprehension” method is used. The “string.lower()” method is an easy way to convert the string elements of the list into lowercase. Similarly, the “map()” function and the “List Comprehension” approach are used along with the various other built-in functionalities to apply the desired functionality. This Python guide presented multiple ways to convert Python list elements into lowercase using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply