Apply function to each element of a list - Python Last Updated : 18 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Applying a function to a list means performing a specific operation or transformation on each element of the list. For instance, we might want to multiply each element of a list by a constant or perform any custom operation using a function. In this article, we will explore various ways to Apply function to each element of a list. Using map() map() methods take two arguments: iterables and functions and return a map object. We use list() to convert the map object to a list. Python # Function to square a number def sq(x): return x * x a = [1, 2, 3, 4] # Applying the square function to each element a= list(map(sq, a)) print(a) Output[1, 4, 9, 16] Explanation:The map() function applies the sq() function to each element of the list a.The result of map() is converted into a list using list(), creating a new list of squared numbers.Let's explore some other methods to apply function to each element of a list in PythonTable of ContentUsing list ComprehensionUsing a for LoopUsing lambda with map()Using list ComprehensionWe use a list comprehension to call a function on each element of the list and then square it for this case. Python a = [1, 2, 3, 4] # Applying the square function using list comprehension b = [x * x for x in a] print(b) Output[1, 4, 9, 16] Explanation:This list comprehension loops through each element x in the list a.For each element, it calculates the square (x * x) and adds it to the new list sq_num.Using for LoopA traditional for loop can also be used to apply a function to each element of a list. Here The loop iterates will through the list, applies the double function to each element and appends the result to a new list. Python # Function to double a number def double(x): return x * 2 a = [1, 2, 3, 4] # Using a for loop to double the elements b = [] for num in a: b.append(double(num)) print(b) Output[2, 4, 6, 8] Explanation:double() function multiplies each element in the list a by 2.loop iterates over each element of the list [1, 2, 3, 4] and appends the doubled value to the doubled_numbers list.The final list doubled_numbers contains [2, 4, 6, 8], which is printed.Using lambda with map()Instead of defining a separate function we can use a lambda function directly with map(). This eliminates the need for a separately defined function.Example: Python a = [1, 2, 3, 4] # Using lambda to triple each number tripled_nums = list(map(lambda x: x * 3, a)) print(tripled_nums) Output[3, 6, 9, 12] Explanation: x * 3 function triples each element in the list [1, 2, 3, 4].Using map() is applied to every element, and list() converts the result into a list.The final list contains [3, 6, 9, 12] after each element is multiplied by 3. Comment More infoAdvertise with us Next Article Apply function to each element of a list - Python P pulamolusaimohan Follow Improve Article Tags : Python python-list Practice Tags : pythonpython-list Similar Reads Divide all Elements of a List by a Number in Python The task of dividing all elements of a list by a number in Python involves iterating over the list and applying the division operation to each element. For example, given a list a = [14, 8, 0, 12, 981, 21, -99] and a divisor d = 7, the result after dividing each element by 7 will be [2, 1, 0, 1, 140 3 min read How to add Elements to a List in Python In Python, lists are dynamic which means that they allow further adding elements unlike many other languages. In this article, we are going to explore different methods to add elements in a list. For example, let's add an element in the list using append() method:Pythona = [1, 2, 3] a.append(4) prin 2 min read Find average of a list in python We are given a list of numbers and the task is to find the average (or mean) value of its elements. For example, if we have a list nums = [10, 20, 30, 40], the sum of the elements is 10 + 20 + 30 + 40, which equals 100. The number of elements in the list is 4. So, the average is calculated as 100 / 2 min read Capitalize Each String in a List of Strings in Python In Python, manipulating strings is a common task, and capitalizing each string in a list is a straightforward yet essential operation. This article explores some simple and commonly used methods to achieve this goal. Each method has its advantages and use cases, providing flexibility for different s 3 min read Remove all the occurrences of an element from a list in Python The task is to perform the operation of removing all the occurrences of a given item/element present in a list. Example Input1: 1 1 2 3 4 5 1 2 1 Output1: 2 3 4 5 2 Explanation : The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1. After removing the item, the output list is [ 4 min read Like