# 23.6. Exercises¶

1. Write equivalent code using map instead of the manual accumulation below and assign it to the variable test.

1. Use manual accumulation to define the lengths function below.

1. Now define lengths using map instead.

1. Now define lengths using a list comprehension instead.

1. 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.

1. 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.

1. 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.

1. Define longwords using manual accumulation.

1. Define longwords using filter.

1. Define longwords using a list comprehension.

1. Write a function called longlengths that returns the lengths of those strings that have at least 4 characters. Try it with a list comprehension.

1. Write a function called longlengths that returns the lengths of those strings that have at least 4 characters. Try it using map and filter.

1. 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.

1. 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.

1. 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.

1. 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].

1. Write code to assign to the variable compri_sample all the values of the key name in the dictionary tester if they are Juniors. Do this using list comprehension.

1. 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. Assign it to the variable result2.

1. Challenge: Write code to assign to the variable class_sched all the values of the key important classes. Do this using list comprehension.

1. Challenge: Below, we have provided a list of lists that contain numbers. Using list comprehension, create a new list threes that contains all the numbers from the original list that are divisible by 3. This can be accomplished in one line of code.

## 23.6.1. Contributed Exercises¶

Use dictionary comprehension to filter the dictionary of total coal production. Your dictionary should only contain those counties that actually produced coal. Produce a dictionary named coal_producers where the key is the county name and value is the total production from that county. There are 37 counties that produced coal.

Create a dictionary named coal_total that contains the total amount of coal produced in each county. The key should be the county name and the value should be the total tons of coal mined.

Write a list comprehension to filter the values from the values list so it includes values that are at least 0 but not more than 100. Place the results in a new list named filtered_values.

The loop below constructs the list b starting from list a. Use list comprehension to create a new list called b2 from list a. Your list, b2, should be equivalent to b. The only difference is that you are creating it using a list comprehension.