::: {#navbar .navbar .navbar-default .navbar-fixed-top role="navigation"}
::: {.container}
::: {.navbar-header}
[]{.icon-bar} []{.icon-bar} []{.icon-bar}

<div>

[![](../_static/img/RAIcon.png)](/runestone/default/user/login){.brand-logo}
[fopp](../index.html){#rs-book-toc-link .navbar-brand}

</div>
:::

::: {.navbar-collapse .collapse .navbar-ex1-collapse}
-   
-   []{#ad_warning}
-   []{#browsing_warning}
-   
-   [*[Search]{.visuallyhidden
    aria-label="Search"}*](#){.dropdown-toggle}
    -   [Table of Contents](../index.html)

    -   [Book Index](../genindex.html)

    -   

    -   ::: {.input-group}
        :::
-   
-   [*[User]{.visuallyhidden aria-label="User"}*](#){.dropdown-toggle}
    -   []{.loggedinuser}

    -   

    -   [Course Home](/ns/course/index)

    -   [Assignments](/assignment/student/chooseAssignment)

    -   [Practice](/runestone/assignments/practice)

    -   [[Peer Instruction
        (Instructor)](/runestone/peer/instructor.html)]{#inst_peer_link}

    -   [Peer Instruction (Student)](/runestone/peer/student.html)

    -   

    -   [Change Course](/runestone/default/courses)

    -   

    -   [[Instructor\'s
        Page](/runestone/admin/index)]{#ip_dropdown_link}

    -   [Progress Page](/runestone/dashboard/studentreport)

    -   

    -   [Edit Profile](/runestone/default/user/profile){#profilelink}

    -   [Change
        Password](/runestone/default/user/change_password){#passwordlink}

    -   [Register](/runestone/default/user/register){#registerlink}

    -   [Login](#)

    -   ::: {.slider .round}
        :::

        Dark Mode
-   
-   [[*[Scratch Activecode]{.visuallyhidden
    aria-label="Scratch Activecode"}*](javascript:runestoneComponents.popupScratchAC())]{#scratch_ac_link}
-   
-   [*[Help]{.visuallyhidden aria-label="Help"}*](#){.dropdown-toggle}
    -   [FAQ](http://runestoneinteractive.org/pages/faq.html)
    -   [Instructors Guide](https://guide.runestone.academy)
    -   
    -   [About Runestone](http://runestoneinteractive.org)
    -   [Report A
        Problem](/runestone/default/reportabug?course=fopp&page=Exercises)
-   

```{=html}
<!-- -->
```
-   
-   [This Chapter](../index.html){.dropdown-toggle}
    -   [23.1 Introduction: Map, Filter, List Comprehensions, and
        Zip](intro.html){.reference .internal}
    -   [23.2 Map](map.html){.reference .internal}
    -   [23.3 Filter](filter.html){.reference .internal}
    -   [23.4 List Comprehensions](listcomp.html){.reference .internal}
    -   [23.5 Zip](zip.html){.reference .internal}
    -   [23.6 Exercises](Exercises.html){.reference .internal}
    -   [23.7 Chapter Assessment](ChapterAssessment.html){.reference
        .internal}
-   
-   
:::
:::
:::

::: {#continue-reading .container}
:::

::: {#main-content .container role="main"}
::: {#exercises .section}
[23.6. ]{.section-number}Exercises[¶](#exercises "Permalink to this heading"){.headerlink}
==========================================================================================

::: {#q21_6_1 .full-width .container .question component="question" question_label="23.6.1"}
1.  ::: {#q1 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_1 component="activecode" question_label="23.6.2"}
    ::: {#ac21_6_1_question .ac_question}
    Write equivalent code using map instead of the manual accumulation
    below and assign it to the variable `test`{.docutils .literal
    .notranslate}.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_2 .full-width .container .question component="question" question_label="23.6.3"}
2.  ::: {#q2 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_2 component="activecode" question_label="23.6.4"}
    ::: {#ac21_6_2_question .ac_question}
    Use manual accumulation to define the lengths function below.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_3 .full-width .container .question component="question" question_label="23.6.5"}
3.  ::: {#q3 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_3 component="activecode" question_label="23.6.6"}
    ::: {#ac21_6_3_question .ac_question}
    Now define lengths using map instead.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_4 .full-width .container .question component="question" question_label="23.6.7"}
4.  ::: {#q4 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_4 component="activecode" question_label="23.6.8"}
    ::: {#ac21_6_4_question .ac_question}
    Now define lengths using a list comprehension instead.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_5 .full-width .container .question component="question" question_label="23.6.9"}
5.  ::: {#q5 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_5 component="activecode" question_label="23.6.10"}
    ::: {#ac21_6_5_question .ac_question}
    Write a function called positives\_Acc that receives list of numbers
    as the input (like \[3, -1, 5, 7\]) and returns a list of only the
    positive numbers, \[3, 5, 7\], via manual accumulation.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_6 .full-width .container .question component="question" question_label="23.6.11"}
6.  ::: {#q6 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_6 component="activecode" question_label="23.6.12"}
    ::: {#ac21_6_6_question .ac_question}
    Write a function called positives\_Fil that receives list of things
    as the input and returns a list of only the positive things, \[3, 5,
    7\], using the filter function.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_7 .full-width .container .question component="question" question_label="23.6.13"}
7.  ::: {#q7 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_7 component="activecode" question_label="23.6.14"}
    ::: {#ac21_6_7_question .ac_question}
    Write a function called positives\_Li\_Com that receives list of
    things as the input and returns a list of only the positive things,
    \[3, 5, 7\], using the list comprehension.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_8 .full-width .container .question component="question" question_label="23.6.15"}
8.  ::: {#q8 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_8 component="activecode" question_label="23.6.16"}
    ::: {#ac21_6_8_question .ac_question}
    Define longwords using manual accumulation.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_9 .full-width .container .question component="question" question_label="23.6.17"}
9.  ::: {#q9 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_9 component="activecode" question_label="23.6.18"}
    ::: {#ac21_6_9_question .ac_question}
    Define longwords using filter.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_10 .full-width .container .question component="question" question_label="23.6.19"}
10. ::: {#q10 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_10 component="activecode" question_label="23.6.20"}
    ::: {#ac21_6_10_question .ac_question}
    Define longwords using a list comprehension.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_11 .full-width .container .question component="question" question_label="23.6.21"}
11. ::: {#q11 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_11 component="activecode" question_label="23.6.22"}
    ::: {#ac21_6_11_question .ac_question}
    Write a function called `longlengths`{.docutils .literal
    .notranslate} that returns the lengths of those strings that have at
    least 4 characters. Try it with a list comprehension.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_12 .full-width .container .question component="question" question_label="23.6.23"}
12. ::: {#q12 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_12 component="activecode" question_label="23.6.24"}
    ::: {#ac21_6_12_question .ac_question}
    Write a function called `longlengths`{.docutils .literal
    .notranslate} that returns the lengths of those strings that have at
    least 4 characters. Try it using map and filter.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_13 .full-width .container .question component="question" question_label="23.6.25"}
13. ::: {#q13 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_13 component="activecode" question_label="23.6.26"}
    ::: {#ac21_6_13_question .ac_question}
    Write a function that takes a list of numbers and returns the sum of
    the squares of all the numbers. Try it using an accumulator pattern.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_14 .full-width .container .question component="question" question_label="23.6.27"}
14. ::: {#q14 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_14 component="activecode" question_label="23.6.28"}
    ::: {#ac21_6_14_question .ac_question}
    Write a function that takes a list of numbers and returns the sum of
    the squares of all the numbers. Try it using map and sum.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_15 .full-width .container .question component="question" question_label="23.6.29"}
15. ::: {#q15 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_15 component="activecode" question_label="23.6.30"}
    ::: {#ac21_6_15_question .ac_question}
    Use the zip function to take the lists below and turn them into a
    list of tuples, with all the first items in the first tuple, etc.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_16 .full-width .container .question component="question" question_label="23.6.31"}
16. ::: {#q16 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_16 component="activecode" question_label="23.6.32"}
    ::: {#ac21_6_16_question .ac_question}
    Use zip and map or a list comprehension to make a list consisting
    the maximum value for each position. For L1, L2, and L3, you would
    end up with a list \[4, 5, 3, 5\].
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_17 .full-width .container .question component="question" question_label="23.6.33"}
17. ::: {#q17 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_17 component="activecode" question_label="23.6.34"}
    ::: {#ac21_6_17_question .ac_question}
    Write code to assign to the variable `compri_sample`{.docutils
    .literal .notranslate} all the values of the key name in the
    dictionary `tester`{.docutils .literal .notranslate} if they are
    Juniors. Do this using list comprehension.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_18 .full-width .container .question component="question" question_label="23.6.35"}
18. ::: {#q18 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_18 component="activecode" question_label="23.6.36"}
    ::: {#ac21_6_18_question .ac_question}
    **Challenge** The nested for loop given takes in a list of lists and
    combines the elements into a single list. Do the same thing using a
    list comprehension for the list `L`{.docutils .literal
    .notranslate}. Assign it to the variable `result2`{.docutils
    .literal .notranslate}.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_19 .full-width .container .question component="question" question_label="23.6.37"}
19. ::: {#q19 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_19 component="activecode" question_label="23.6.38"}
    ::: {#ac21_6_19_question .ac_question}
    **Challenge:** Write code to assign to the variable
    `class_sched`{.docutils .literal .notranslate} all the values of the
    key `important classes`{.docutils .literal .notranslate}. Do this
    using list comprehension.
    :::
    :::
    :::
    :::
    :::
:::

::: {#q21_6_20 .full-width .container .question component="question" question_label="23.6.39"}
20. ::: {#q20 .alert .alert-warning component="tabbedStuff"}
    ::: {component="tab" tabname="Question"}
    ::: {.runestone .explainer .ac_section}
    ::: {#ac21_6_20 component="activecode" question_label="23.6.40"}
    ::: {#ac21_6_20_question .ac_question}
    **Challenge:** Below, we have provided a list of lists that contain
    numbers. Using list comprehension, create a new list
    `threes`{.docutils .literal .notranslate} that contains all the
    numbers from the original list that are divisible by 3. This can be
    accomplished in one line of code.
    :::
    :::
    :::
    :::
    :::
:::

::: {#contributed-exercises .section}
[23.6.1. ]{.section-number}Contributed Exercises[¶](#contributed-exercises "Permalink to this heading"){.headerlink}
--------------------------------------------------------------------------------------------------------------------
:::
:::

::: {style="width: 100%"}
::: {ea-publisher="runestoneacademy" ea-type="image" style="display: flex; justify-content: center"}
:::
:::

::: {#scprogresscontainer}
You have attempted []{#scprogresstotal} of []{#scprogressposs}
activities on this page

::: {#subchapterprogress aria-label="Page progress"}
:::
:::

-   [[](zip.html)]{#relations-prev}
-   [[](ChapterAssessment.html)]{#relations-next}
:::

::: {.container}
[]{#numuserspan}[]{.loggedinuser} \| [Back to top](#)

© Copyright 2017 bradleymiller. Created using
[Runestone](http://runestoneinteractive.org/) 7.2.10.
:::
