Python: Exercises


4. The zip function. In Python, the zip() function can be used to combine multiple iterator objects, such as lists.

If we have two lists, a and b, we can combine them with the command zip(a,b).

In this exercise, you will define two lists, zip them together, iterate over the zipped object, and print its values.


# Define a list of integers between 1 and 3. integer_list = ___ # Define list of lists. list_of_lists = [[1, 2], [], ["a",7]] # Zip the list of integers and lists of lists together. zipped_lists = ___ # Iterate over list and print values. for element in zipped_list: print(element) # Define a list of integers between 1 and 3. integer_list = [1, 2, 3] # Define list of lists. list_of_lists = [[1, 2], [], ["a",7]] # Zip the list of integers and lists of lists together. zipped_lists = zip(integer_list, list_of_lists) # Iterate over list and print values. for element in zipped_lists: print(element) test_object("integer_list") test_function("zip") success_msg("Excellent work. Notice that a for loop in Python doesn't use either an end statement or brackets.")
Remember that you can zip two iterators, a and b together using the zip() function.

Previous Exercise Next Exercise