Extra Exercises

  1. Using map, create a list assigned to the variable greeting_doubled that doubles each element in the list lst.

1.1 Write code to assign to the variable map_testing all the elements in lst_check while adding the string “Fruit: ” to the beginning of each element using mapping.

1.2 Below, we have provided a list of strings called abbrevs. Use map to produce a new list called abbrevs_upper that contains all the same strings in upper case.

  1. Using filter, filter lst so that it only contains words containing the letter “o”. Assign to variable lst2. Do not hardcode this.

2.1 Write code to assign to the variable filter_testing all the elements in lst_check that have a w in them using filter.

2.2 Below, we have provided a list of strings called countries. Use filter to produce a list called b_countries that only contains the strings from countries that begin with B.

  1. Using reduce, join the strings in the list, str_lst, with the symbol “&” and assign the variable joined. Then, combine joined with the string beg so that the final string reads “I like apples & peaches & oranges & grapes & pineapples”. Save this final string in the variable final. Do not hardcode.

3.1 Write code to assign to the variable reducing_testing all the elements in lst_check and compiling them in a string that begins with “Fruits: ” and has a comma and a space between each element using reduce.

3.2 Below, we have provided a list of integers called nums. Use reduce to produce a variable, product, that is all of the integers in nums multiplied together.

  1. The for loop below produces a list of numbers greater than 10. Below the given code, use list comprehension to accomplish the same thing. Assign it the the variable lst2. Only one line of code is needed.

4.1 Write code to assign to the variable compri all the values of the key name in the dictionary tester. Do this using list comprehension.

4.2 Below, we have provided a list of tuples that contain the names of Game of Thrones characters. Using list comprehension, create a list of strings called first_names that contains only the first names of everyone in the original list.

  1. Use list comprehension to create a list called lst2 that doubles each element in the list, lst.

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.

5.2 Below, we have provided a list of tuples that contain students’ names and their final grades in PYTHON 101. Using list comprehension, create a new list passed that contains the names of students who passed the class (had a final grade of 70 or greater).

  1. Below we have provided two lists of numbers, L1 and L2. Using zip and list comprehension, create a new list, L3, that sums the two numbers if the number from L1 is greater than 10 and the number from L2 is less than 5. This can be accomplished in one line of code.

6.1 Write code using zip and filter so that these lists (l1 and l2) are combined into one big list and assigned to the variable opposites if they are both longer than 3 characters each.

6.2 Below, we have provided a species list and a population list. Use zip to combine these lists into one list of tuples called pop_info. From this list, create a new list called endangered that contains the names of species whose populations are below 2500.

  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.

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

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

Next Section - Facebook API Exercises