QuizzesLambda, map, filter & sorted key functions — Concept Check
intermediate
python

Lambda, map, filter & sorted key functions — Concept Check

Questions on Lambda, map, filter & sorted key functions. lambda creates small anonymous functions inline — most powerful as the key= argument to sorted/min/max, and alongside map/filter for quick transformations.

7 minutes 7 questions Pass at 70%
Time Limit
7 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

7 questions on Lambda, map, filter & sorted key functions — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a list of dictionaries representing employees with 'name' and 'age'. How would you sort this list by age in ascending order using lambda?

    • Asorted(employees, key=lambda e: e['name'])
    • Bsorted(employees, key=lambda e: e['age'], reverse=True)
    • Csorted(employees, key=lambda e: -e['age'])
    • Dsorted(employees, key=lambda e: e['age'])
  2. 2.Given a list of numbers, nums = [5, 3, 8, 1], which lambda function with map would double each number?

    • Alist(map(lambda x: x - 2, nums))
    • Blist(map(lambda x: x / 2, nums))
    • Clist(map(lambda x: x * 2, nums))
    • Dlist(map(lambda x: x + 2, nums))
  3. 3.A developer uses filter with lambda to get odd numbers from a list. Which option correctly achieves this?

    • Afilter(lambda x: x % 2 == 0, nums)
    • Bfilter(lambda x: x % 2 != 0, nums)
    • Cfilter(lambda x: x > 0, nums)
    • Dfilter(lambda x: x < 0, nums)
  4. 4.You need to find the student with the highest CGPA from a list using lambda. Which function should you use?

    • Asorted(students, key=lambda s: s['cgpa'], reverse=True)[-1]
    • Bsorted(students, key=lambda s: s['cgpa'])[0]
    • Cmin(students, key=lambda s: s['cgpa'])
    • Dmax(students, key=lambda s: s['cgpa'])
  5. 5.When sorting words by length, which lambda function would also sort alphabetically in case of ties?

    • Asorted(words, key=lambda w: (-len(w), w))
    • Bsorted(words, key=lambda w: w)
    • Csorted(words, key=lambda w: (len(w), w))
    • Dsorted(words, key=lambda w: len(w))
  6. 6.What happens when you use a lambda function with sorted to sort a list of tuples by the second element?

    • Asorted(list_of_tuples, key=lambda x: x[1])
    • Bsorted(list_of_tuples, key=lambda x: x[1], reverse=True)
    • Csorted(list_of_tuples, key=lambda x: -x[1])
    • Dsorted(list_of_tuples, key=lambda x: x[0])
  7. 7.Why is assigning a lambda to a variable considered an anti-pattern in Python?

    • AIt makes the code run slower.
    • BIt goes against PEP 8 recommendations.
    • CIt prevents the use of map and filter.
    • DIt is the only way to use lambda functions.