Transform List using Python map()

How to transform a list using the python map() function? We are often required to transform every element of the list and return a new list. Python provides several ways to transform a list, using map() is one of the most used to transform iterable objects including a list.

In this article, first, will see how to apply the transformation in the traditional way using for loop and then use the map(), and finally, use a map along with lambda expression.

1. Transform List using for Loop

Below is an example to transform a list using python for loop. When using loops, we need to iterate to get each element from the list, apply the transformation and add the result to a new list.

python transform list map
Python List Transform

2. Using map() to Transform List

The map() will execute a function for every element of the iterable and returns a new iterable object. So, we can transform the list into a new list by applying a function to each element of the list using a python map().


# Syntax of map() function
map(function, iterable, [iterable1, iterable2, ...])

# Parameters
* function – A function is a callable object (i.e. a function, method, or 
     lambda expression) that will be applied to each item 
     of the iterable (e.g. list, tuple, etc.).
* iterable – It is iterable which is the collection of items on 
     which the function will be applied. You can use one or more iterables.

Let’s write the above example by using the map() to transform the list.


# Using map() to transform list

# Create numbers list
numbers = {2,3,4,5,6}
print("Original:", numbers)

# Square function
def square(x):
    return x * x

# using map() to transform
result = list(map(square,numbers))
print("Result: ",result)

# Output:
# Original: [2, 3, 4, 5, 6]
# Result:  [4, 9, 16, 25, 36]

Here, the square() function is executed for each element in the numbers list. The square() function takes a single argument x, and returns the value of x * x. When the map() function is called with square() function and numbers as arguments, it applies the square() function to each element in the numbers list and returns an iterator object. The list() function is then used to convert the iterator to a list.

3. Using map() & Lambda to Transform List

You can also write the above code by using map() and lambda function. The lambda is an anonymous function that is used to apply the transformations on an iterable object like a list, set, etc.

Below is the syntax of the lambda expression.


# Syntax of using map() with lambda
map(lambda arguments: expresion, iterable1, [iterable2,...])

Let’s re-write the above code using a map and lambda expression to transform the list in python. The lambda can take any number of arguments, but can only have one expression. If no value is returned from the lambda expression, it returns None.


# By using map() & lambda
result = list(map(lambda x: x * x, numbers))
print("Result: ",result)

# Output:
# Result:  [4, 9, 16, 25, 36]

You can also with this with the lambda function. Here, we create a lambda expression and assign it a square variable. And use this variable on the map().


# Create lambda function and assign it to variable
square = lambda x: x * x

# Using map() with lambda
result = list(map(square, numbers))

# Output:
# Result:  [4, 9, 16, 25, 36]

4. Transform List of Strings to Lowercase

The below example uses the map() and lambda to apply a transformation function lower() to convert each string element of the list.


# Create list of strings
myStrings = ["PYTHON","JAVA","PHP","PANDAS"]
print("Original:",myStrings)

# Trasnsform string to lower
result = list(map(lambda x:x.lower(), myStrings))
print("Result: ",result)

# Output:
Original: ['PYTHON', 'JAVA', 'PHP', 'PANDAS']
Result:  ['python', 'java', 'php', 'pandas']

5. Transform List of Lists

You can also transform list of lists or list of tuples, let’s create a list with another list that represents as student. Imagine student has elements in the following order id, name, and fee. By using list transformation I will add a another element by calculating 15% of the fee. Here is an example.


# Transform list of lists
students = [[1,"Ram", 5000],[1,"Scott", 4000]]
print("Original:", students)
students = list(map(lambda student:[student[0],student[1],student[2],(student[2]*15/100) ], students))
print("Result",students)

# Output:
# Original: [[1, 'Ram', 5000], [1, 'Scott', 4000]]
# Result [[1, 'Ram', 5000, 750.0], [1, 'Scott', 4000, 600.0]]

5. Using List Comprehension

With python list comprehension, we can create lists by specifying the elements. We select elements that we want to include, along with any conditions or operations. All this is done in a single line of code.


result = [x.lower() for x in myStrings]
print(result)

# Output:
# Result:  ['python', 'java', 'php', 'pandas']

Conclusion

In this article, you have learned how to transform the list using the map(). First, we saw how cumbersome it is to use for loop to transform the list, and then same code we re-write using the map() and finally used the map with lambda to transform the list.

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply