Complete the code to combine two lists into pairs using the zip() function.
list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] combined = [1](list1, list2) print(list(combined))
The zip() function pairs elements from two lists together. Here, it combines list1 and list2 into pairs.
Complete the code to print pairs of elements from two lists using zip() in a for loop.
names = ['Alice', 'Bob', 'Charlie'] ages = [25, 30, 35] for [1], age in zip(names, ages): print(f"{name} is {age} years old")
The variable name holds each element from the names list during iteration. It matches the first item from each pair created by zip().
Fix the error in the code by completing the blank to correctly unzip a list of pairs.
pairs = [(1, 'a'), (2, 'b'), (3, 'c')] numbers, letters = zip(*[1]) print(numbers) print(letters)
The * operator unpacks the list of pairs. We need to unpack the variable pairs to unzip the elements into separate tuples.
Fill both blanks to create a dictionary from two lists using zip() and a dictionary comprehension.
keys = ['name', 'age', 'city'] values = ['Alice', 30, 'New York'] info = [1]([2] for [2] in zip(keys, values)) print(info)
The zip(keys, values) pairs keys and values. The dictionary constructor dict() creates a dictionary from these pairs. The variable item represents each pair in the comprehension.
Fill all three blanks to filter pairs from two lists where the number is greater than 2 using zip() and a dictionary comprehension.
nums = [1, 2, 3, 4] letters = ['a', 'b', 'c', 'd'] filtered = [1]([2]: [3] for [2], letter in zip(nums, letters) if [2] > 2) print(filtered)
The dictionary comprehension creates a dictionary from pairs where the number is greater than 2. num is the variable for numbers, letter for letters, and dict() builds the dictionary.