MulaiMulai sekarang secara gratis

Using zip

Another interesting function that you've learned is zip(), which takes any number of iterables and returns a zip object that is an iterator of tuples. If you wanted to print the values of a zip object, you can convert it into a list and then print it. Printing just a zip object will not return the values unless you unpack it first. In this exercise, you will explore this for yourself.

Three lists of strings are pre-loaded: mutants, aliases, and powers. First, you will use list() and zip() on these lists to generate a list of tuples. Then, you will create a zip object using zip(). Finally, you will unpack this zip object in a for loop to print the values in each tuple. Observe the different output generated by printing the list of tuples, then the zip object, and finally, the tuple values in the for loop.

Latihan ini adalah bagian dari kursus

Python Toolbox

Lihat Kursus

Petunjuk latihan

  • Using zip() with list(), create a list of tuples from the three lists mutants, aliases, and powers (in that order) and assign the result to mutant_data.
  • Using zip(), create a zip object called mutant_zip from the three lists mutants, aliases, and powers.
  • Complete the for loop by unpacking the zip object you created and printing the tuple values. Use value1, value2, value3 for the values from each of mutants, aliases, and powers, in that order.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Create a list of tuples: mutant_data
mutant_data = ____

# Print the list of tuples
print(mutant_data)

# Create a zip object using the three lists: mutant_zip
mutant_zip = ____

# Print the zip object
print(mutant_zip)

# Unpack the zip object and print the tuple values
for ____ in ____:
    print(value1, value2, value3)
Edit dan Jalankan Kode