In this exercise, we will see how we can combine the
Conceptually, if you’re working with a collection of items and find yourself saying, “I need to map only values that have property x,” you will likely need to use
filter() together. in English, “I need to map filtered values” translates into Python like this:
map(mapping_function, filter(predicate, iterable))
Let’s take a look at an example.
We have a tuple representing the records of students in various math classes. The tuple will be structured in the following way:
student(name, grade, course). As an example,
student("Peter", 'B', 101) will represent a student named Peter who received a grade of B in a Math 101 course.
We want to register students who receive a grade of B or better in their respective math courses for a special advanced math course: Math 201. To create the record for the course, we will filter all students with grade B or higher and map their math course to 201.
The initial grade in the new tuple will contain the letter ‘X’ to represent that it is not yet defined because the student has not yet finished Math 101. We can do this in Python like so:
First, we create a
namedtuple to represent a student:
from collections import namedtuple # Create a class called student student = namedtuple("student", ["name", "grade", "course_number"])
We then create records for the sample data:
# Create the records for the students in the form of tuples peter = student("Peter", 'B', 101) amanda = student("Amanda", 'C', 101 ) sarah = student("Sarah", 'A', 102) lisa = student("Lisa", 'D', 101) alex = student("Alex", 'A', 102) maria = student("Maria", 'B', 101) andrew = student("Andrew", 'C', 102) math_class = (peter, amanda, sarah, lisa, alex, maria, andrew)
Finally, we use
map() to create a new record for a student consisting of:
- their name
- the initial grade X
- the math course Math 201 only IF the student’s previous grade was B or above in their respective math course
math_201 = map(lambda s: student(s.name, 'X', 201), filter(lambda q: q.grade <= 'B', math_class)) print(tuple(math_201))
q.grade <= 'B' removes students with grades less than B. The
<= appears because the character A is lower than B in the alphabet (how Python compares the characters).
The code “maps a filtered value”. Remember, that because
map() returns an iterable, we use the
tuple() function to generate all the values we need.
The output will be all the students who are eligible for Math 201:
(student("Peter, 'X', 201), student("Sarah",' X', 201), student("Alex", 'X', 201), student("Maria", 'X', 201))
Given the tuple
nums multiply values that are greater than 10 by two. Save your result as
greater_than_10_doubled. Print your answer using the following line of code:
Convert the following code from the imperative style to the declarative:
lst =  for i in nums: if i % 3 == 0: lst.append(i) for i in range(len(lst)): lst[i] = lst[i] * 3 tuple(lst)
nums represents the tuple provided in the workspace.
Save your result in a variable called
functional_way. Print your answer using the following line of code:
If you get stuck, look over the hint.