0
0
Pythonprogramming~10 mins

zip() function in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to combine two lists into pairs using the zip() function.

Python
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
combined = [1](list1, list2)
print(list(combined))
Drag options to blanks, or click blank then click option'
Afilter
Brange
Cmap
Dzip
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using range() instead of zip() causes a TypeError when printing.
Using map() or filter() won't combine lists into pairs.
2fill in blank
medium

Complete the code to print pairs of elements from two lists using zip() in a for loop.

Python
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
for [1], age in zip(names, ages):
    print(f"{name} is {age} years old")
Drag options to blanks, or click blank then click option'
Aages
Bnames
Cname
Dage
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'names' instead of 'name' causes confusion because 'names' is the whole list.
Using 'age' or 'ages' for the first variable is incorrect because it matches the second list.
3fill in blank
hard

Fix the error in the code by completing the blank to correctly unzip a list of pairs.

Python
pairs = [(1, 'a'), (2, 'b'), (3, 'c')]
numbers, letters = zip(*[1])
print(numbers)
print(letters)
Drag options to blanks, or click blank then click option'
Apairs
Blist
Czip
Denumerate
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'list' causes a TypeError because it's a type, not the variable.
Using 'zip' or 'enumerate' causes errors because they are functions, not the data.
4fill in blank
hard

Fill both blanks to create a dictionary from two lists using zip() and a dictionary comprehension.

Python
keys = ['name', 'age', 'city']
values = ['Alice', 30, 'New York']
info = [1]([2] for [2] in zip(keys, values))
print(info)
Drag options to blanks, or click blank then click option'
Adict
Bitem
Citems
Dlist
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'list' instead of 'dict' creates a list of tuples, not a dictionary.
Using 'items' instead of 'item' causes a NameError because the variable is undefined.
5fill in blank
hard

Fill all three blanks to filter pairs from two lists where the number is greater than 2 using zip() and a dictionary comprehension.

Python
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)
Drag options to blanks, or click blank then click option'
Adict
Bnum
Cletter
Ditem
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'item' instead of 'num' or 'letter' causes confusion and errors.
Not using dict() results in a generator object, not a dictionary.