FAQ: Functional Programming - Review of filter(), map(), and reduce()

This community-built FAQ covers the “Review of filter(), map(), and reduce()” exercise from the lesson “Functional Programming”.

Paths and Courses
This exercise can be found in the following Codecademy content:

[Beta] Learn Advanced Python 3

FAQs on the exercise Review of filter(), map(), and reduce()

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello!

I have found some mistakes in the lesson code snippets.

nums = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 
# filter_values is not a higher-order function
def filter_values(predicate, lst):
 
  # Mutable list required because this example is imperative, not declarative
  ret = []
  for i in lst:
    if predicate(i):
      ret.append(i)
  return ret
 
filtered_numbers = filter_values(lambda x: x % 2 == 0, numbers) 
 
print(k) 
 
# This will output the tuple: (2, 4, 6, 8, 10)

“nums” became “numbers” in the end;
“filetered_numbers” became “k” in the end;
“This will output the tuple” will not output the tuple, according to “return ret” statement (ret is a list).

nums = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 
filtered_numbers = filter(lambda x: x % 2 == 0, numbers) 
 
print(tuple(filtered_numbers))
 
# This will output the tuple: (2, 4, 6, 8, 10)

“nums” became "numbers.

nums = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 
def mapper(function, lst):
  ret = []
  for i in lst:
    ret.append(function(i))
  return ret
 
mapped_numbers  = mapper(lambda x: x*x, numbers)
 
print(tuple(k))
 
# This will output: (1, 4, 9, 16, 25, 36, 49, 64, 81, 100)

“nums” became “numbers” in the end;
“mapped_numbers” became “k” in the end.

nums = (2, 6, 7, 9, 1, 4, 8)
 
sum = 0
 
for i in t:
  sum += i
 
print(sum) # Output: 37

“nums” became “t”

from functools import reduce
 
nums = (2, 6, 7, 9, 1, 4, 8)
 
k = reduce(lambda x, y: x + y, t) # k is a number
 
print(k) # Output: 37

“nums” became “t”.

Hope this information will help somehow.

2 Likes

filtered_numbers = filter(lambda x: not x%2, nums) does not work, had to type filtered_numbers = filter(lambda x: x%2 == 0, nums) despite the outputs being the same.