Python: Exercises


3. List comprehensions. In Python, it is possible to loop over all of the elements of a list using something called a list comprehension. The syntax is given below for code that loops over each element of list_of_numbers and computes each element's square root.

list_of_sqrts = [sqrt(number) for number in list_of_numbers]

Use a list comprehension to loop over the elements of list_of_numbers and divide each by 5.0.


a_list = [1, 2, 3, 4, 5] # Loop over elements of a_list and divide each by 5.0. a_new_list = [x/___ for ___ in ___] # Print the new list. print(a_new_list) ___ # Loop over elements of a_list and divide each by 5.0. a_new_list = [x/5.0 for x in a_list] # Print the new list. print(a_new_list) test_object("a_new_list") success_msg("Excellent work.")
Review the definition of list comprehension provided in the problem description.

Previous Exercise Next Exercise