The Map Function#
This tutorial will introduce you to the map()
function in Python, with a focus on working with strings. We’ll use examples from Edgar Allan Poe’s works to demonstrate the concepts in a manner that is relevant to humanists.
Example of a Problem#
Imagine we wanted to count the length of every line in Edgar Allan Poe’s famous poem, “The Raven.” Let’s try and solve this problem. First, let’s create a list containing a few lines from the poem:
lines = [
"Once upon a midnight dreary, while I pondered, weak and weary,",
"Over many a quaint and curious volume of forgotten lore—",
"While I nodded, nearly napping, suddenly there came a tapping,",
"As of some one gently rapping, rapping at my chamber door.",
]
Now that we have our data, let’s count each line with a for
loop.
line_lengths_list = []
for line in lines:
line_length = len(line)
line_lengths_list.append(line_length)
print(line_lengths_list)
[62, 56, 62, 58]
By using a for loop, we iterate through each line in the lines
list, calculate the length of each line with len(line)
, and append the result to the line_lengths_list
. In this tutorial, we will learn about how the map()
function can make this problem far simpler to solve with far less code.
What is the map()
function?#
The map()
function is a built-in Python function that allows you to apply a function to every item in an iterable (such as a list or tuple) and returns an iterable map
object, which can be converted into a list or other iterable data structure.
The map()
function has the following syntax:
map(function, iterable)
Basic usage of the map() function with strings#
Let’s start with a simple example. Suppose we have a list of strings containing lines from the same poem. We want to find the length of each line in the poem.
Let’s use the same data.
lines = [
"Once upon a midnight dreary, while I pondered, weak and weary,",
"Over many a quaint and curious volume of forgotten lore—",
"While I nodded, nearly napping, suddenly there came a tapping,",
"As of some one gently rapping, rapping at my chamber door.",
]
Now, let’s use the map()
function to find the length of each line. We’ll use the built-in len()
function as the function to apply to each line:
line_lengths = map(len, lines)
line_lengths
<map at 0x218e8251f40>
At this stage, we have a generator. To learn about generators and why they are important, check out the generator section of this textbook. We can convert this generator into a list and the print it off.
# Convert the map object to a list
line_lengths_list = list(line_lengths)
# Print the result
print(line_lengths_list)
[62, 56, 62, 58]
The map()
function has applied the len() function to each line in the list, and the results have been returned in a new list.
Using custom functions with map()#
Now let’s create a custom function to count number of words in each line with split. We’ll use this function with the map()
function.
First, let’s create the custom function. The purpose of this function is to count the number of words in the line by first splitting the line at any white space. Next, it leverages the len()
function to count the number of words. It then returns the result
def count_words(line):
words = line.split()
num_words = len(words)
return num_words
Now, let’s use the map()
function with our custom function to count the number of words in each line.
word_counts = list(map(count_words, lines))
print(word_counts)
[11, 10, 10, 11]
The map()
function has applied our custom count_words()
function to each line in the list, and the results have been returned in a new list.
Often, when we use map()
, we use it alongside lambda
functions. We will meet those in the next tutorial.
Live Coding#
lines = [
"Once upon a midnight dreary, while I pondered, weak and weary,",
"Over many a quaint and curious volume of forgotten lore—",
"While I nodded, nearly napping, suddenly there came a tapping,",
"As of some one gently rapping, rapping at my chamber door.",
]
lengths = []
for line in lines:
# print(line)
# print(line.split())
lengths.append(len(line.split()))
print(lengths)
[11, 10, 10, 11]
def get_lengths(line):
return len(line.split())
lengths2 = map(get_lengths, lines)
lengths2
<map at 0x218e7f123a0>
lengths_list = list(lengths2)
lengths_list
[11, 10, 10, 11]