Python | Operation to each element in list
Last Updated :
21 Mar, 2024
Given a list, we always come across situations in which we require to apply certain function to each element in a list. This can be easily done by applying a loop and performing an operation to each element. But having shorthands to solve this problem is always beneficial and helps to focus more on important aspects of problem. Let’s discuss certain ways in which each element of list can be operated.
Method #1: Using list comprehension
This method performs the same task in the background as done by the looping constructs. The advantage this particular method provides is that this is a one-liner and also improves code readability.
Python3
# Python3 code to demonstrate
# operations on each list element
# using list comprehension
# initializing list
test_list = ["geeks", "for", "geeks", "is", "best"]
# printing original list
print("The original list is : " + str(test_list))
# operations on each list element
# using list comprehension
# uppercasing each element
res = [i.upper() for i in test_list]
# printing result
print("The uppercased list is : " + str(res))
OutputThe original list is : ['geeks', 'for', 'geeks', 'is', 'best']
The uppercased list is : ['GEEKS', 'FOR', 'GEEKS', 'IS', 'BEST']
Time Complexity: O(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 #2: Using map()
Using map function, one can easily associate an element with the operation one wishes to perform. This is the most elegant way to perform or solve this kind of problems.
Python3
# Python3 code to demonstrate
# operations on each list element
# using map()
# initializing list
test_list = ["Geeks", "foR", "gEEks", "IS", "bEST"]
# printing original list
print("The original list is : " + str(test_list))
# operations on each list element
# using map()
# lowercasing each element
res = list(map(str.lower, test_list))
# printing result
print("The lowercased list is : " + str(res))
OutputThe original list is : ['Geeks', 'foR', 'gEEks', 'IS', 'bEST']
The lowercased list is : ['geeks', 'for', 'geeks', 'is', 'best']
Method #3: Using enumerate and iteration
Another approach that can be used to perform operations on each element in a list is to use the for loop in conjunction with the enumerate() function. The enumerate() function returns an iterator that produces tuples containing the index and value of each element in the list.
Here is an example of using enumerate() and a for loop to convert each element in a list to uppercase:
Python3
# initializing list
test_list = ["geeks", "for", "geeks", "is", "best"]
# printing original list
print("The original list is:", test_list)
# using enumerate() and a for loop to uppercase each element
for i, element in enumerate(test_list):
test_list[i] = element.upper()
# printing result
print("The uppercased list is:", test_list)
OutputThe original list is: ['geeks', 'for', 'geeks', 'is', 'best']
The uppercased list is: ['GEEKS', 'FOR', 'GEEKS', 'IS', 'BEST']
The time complexity of the approach using enumerate() and a for loop to perform an operation on each element in a list is O(n), where n is the number of elements in the list. This is because the loop will iterate over all n elements in the list and perform the operation on each element.
The space complexity of this approach is also O(n), as the original list is modified in-place and no additional space is required to store the result. The modified list will take up the same amount of space as the original list.
Please Login to comment...