23.6. Exercises¶
-
Write equivalent code using map instead of the manual accumulation below and assign it to the variable
test
.
-
Use manual accumulation to define the lengths function below.
-
Now define lengths using map instead.
-
Now define lengths using a list comprehension instead.
-
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.
-
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.
-
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.
-
Define longwords using manual accumulation.
-
Define longwords using filter.
-
Define longwords using a list comprehension.
-
Write a function called
longlengths
that returns the lengths of those strings that have at least 4 characters. Try it with a list comprehension.
-
Write a function called
longlengths
that returns the lengths of those strings that have at least 4 characters. Try it using map and filter.
-
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.
-
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.
-
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.
-
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].
-
Write code to assign to the variable
compri_sample
all the values of the key name in the dictionarytester
if they are Juniors. Do this using list comprehension.
-
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 variableresult2
.
-
Challenge: Write code to assign to the variable
class_sched
all the values of the keyimportant classes
. Do this using list comprehension.
-
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.