209k views
4 votes
Write a Python function that does the following:

Its name is zip_lists
It takes three arguments: list_1, list_2, list_3 – all of the same length
It iterates over all three lists (remember, they have the same length, so you can use the same index for all of them) appending to a new list of tuples of three elements
It returns the list of tuples

User Savageguy
by
7.8k points

1 Answer

4 votes

Final answer:

To create the function zip_lists in Python, use the zip() function along with a list comprehension.

Step-by-step explanation:

To create the function zip_lists in Python, you can use the zip() function along with a list comprehension. Here is an example implementation:

def zip_lists(list_1, list_2, list_3):
return [(x, y, z) for x, y, z in zip(list_1, list_2, list_3)]

# Example usage:
list_1 = [1, 2, 3]
list_2 = ['a', 'b', 'c']
list_3 = [True, False, True]
result = zip_lists(list_1, list_2, list_3)
print(result) # Output: [(1, 'a', True), (2, 'b', False), (3, 'c', True)]

User Kien Vu
by
7.8k points