In this article, we will focus on how to make use of the map()
function in Python to transform data. We will try to use map lambda in python to carry out data transformation.
Lists are a very important data structure in Python programming.
The data contained in them can be manipulated in many different ways.
Contents of a list can be sorted, filtered, or transformed.
Transforming list data in Python
As mentioned in the introduction, transforming list data is an important data manipulation task. One way this can be done is by iterating over the list and converting each item in the list into a new form. The next example will illustrate this method.
In this example, we are going to convert a list of first names in lowercase into a list of names in title case. By title-case, we mean that the first letters of each name are capitalized.
# declare a list of names, all in lowercase
name_list = ['alice', 'bob', 'christy', 'dean', 'esther']
new_name_list = [] # list to store the transformed data
for name in name_list:
new_name_list.append(name.title())
# print out the original list and the transformed list
print("Original list: ", name_list)
print("Transformed list: ", new_name_list)
The above code sample defines a list, new_list
, that contains names in all lowercase. A for-loop
is used in the code to transform each name to title-case. The result of each conversion is stored in the new_name_list
variable. The printout for the code is shown below.
Output:
Original list: ['alice', 'bob', 'christy', 'dean', 'esther']
Transformed list: ['Alice', 'Bob', 'Christy', 'Dean', 'Esther']
While this code does what we need it to do, it could be made shorter by using Python’s map() function.
Python’s map() function
The syntax of python’s map()
function is:
map(function, iterable)
map()
transforms each item in the iterable
parameter using the function
parameter. map()
returns a new iterable consisting of the transformed content of the iterable
parameter. The next example will solve the problem from the previous example using map()
.
# the function used for the transformation
def toTitleCase(name):
return name.title()
# declare a list of names, all in lowercase
name_list = ['alice', 'bob', 'christy', 'dean', 'esther']
# transform name_list using map()
new_name_list = list(map(toTitleCase, name_list))
# print out the original list and the transformed list
print("Original list: ", name_list)
print("Transformed list: ", new_name_list)
The whole transformation task was carried out in a single line where we declared new_name_list
. However, we had to define a function, toTitleCase()
, that would be used by map()
to carry out the item-wise transformation. the result, shown below, is the same as the one we got from the previous example.
Output:
Original list: ['alice', 'bob', 'christy', 'dean', 'esther']
Transformed list: ['Alice', 'Bob', 'Christy', 'Dean', 'Esther']
Notice that we needed to put the call to map()
within a call to the list()
function. This was necessary since the call to map()
returned a map
object that still needed to be converted into a list
object.
Python lambda
The previous example defined a simple, one-off function that map()
needed to make use of. That is, no other part of the code needed to make use of the toTitleCase()
function. Python allows us to define a nameless or anonymous, function that we could make use of just once in map()
. Python calls such functions lambda functions. The syntax of a lambda function is
lambda parameters : expression
The definition of the lambda function begins with the lambda
keyword, instead of def
keyword. We can specify zero or more parameters in the definition of a lambda function. The expression part is required, as this is what the lambda function will evaluate and return when called.
A lambda function can be defined and called immediately, as we see in the next example.
print("The square of 3 is: ", (lambda x: x * x)(3))
Output:
The square of 3 is: 9
Since a lambda function is an object in its own right, we can also save it in a variable, and call it later.
square = lambda x: x * x
print("The square of 3 is: ", square(3))
Output:
The square of 3 is: 9
How to use map and lambda in Python?
We can make use of a lambda function in the map()
function. The final example of this tutorial shows how to do this.
# declare a list of names, all in lowercase
name_list = ['alice', 'bob', 'christy', 'dean', 'esther']
# transform name_list using map() and lambda
new_name_list = list(map((lambda name: name.title()),
name_list))
# print out the original list and the transformed list
print("Original list: ", name_list)
print("Transformed list: ", new_name_list)
Output:
Original list: ['alice', 'bob', 'christy', 'dean', 'esther']
Transformed list: ['Alice', 'Bob', 'Christy', 'Dean', 'Esther']
Since we did not need to reuse the transform function, a lambda function was preferable for our purposes. Also, the output shows the same results as the other examples
Conclusion
Thanks for making it this far!!! You can now make use of map()
function in Python. You can check out our other tutorials on Python.
Keep on learning, and thanks for making us part of your coding journey!!!