0
0
Pythonprogramming~10 mins

Iterator protocol 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 get an iterator from the list.

Python
my_list = [1, 2, 3]
my_iter = [1](my_list)
Drag options to blanks, or click blank then click option'
Alist
Bnext
Citer
Drange
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'iter' to get the iterator.
Trying to convert the list to another list.
Using 'range' which creates a sequence, not an iterator from the list.
2fill in blank
medium

Complete the code to get the next item from the iterator.

Python
my_list = [10, 20, 30]
my_iter = iter(my_list)
item = [1](my_iter)
Drag options to blanks, or click blank then click option'
Aiter
Bnext
Clist
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'iter' again instead of 'next'.
Trying to convert the iterator to a list.
Using 'append' which is a list method, not related to iterators.
3fill in blank
hard

Fix the error in the code to correctly iterate over the list using an iterator.

Python
my_list = [5, 6, 7]
my_iter = iter(my_list)
while True:
    try:
        item = [1](my_iter)
        print(item)
    except StopIteration:
        break
Drag options to blanks, or click blank then click option'
Anext
Biter
Clist
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'iter' inside the loop which resets the iterator.
Using 'list' which is not a function to get next item.
Using 'print' instead of fetching the next item.
4fill in blank
hard

Fill both blanks to create a dictionary of squares for numbers greater than 2.

Python
numbers = [1, 2, 3, 4, 5]
squares = {num: num[1]2 for num in numbers if num [2] 2}
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication (*) instead of exponentiation (**).
Using less than (<) instead of greater than (>).
Confusing the operators for filtering.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 10.

Python
data = {'a': 5, 'b': 15, 'c': 20}
result = { [1]: [2] for k, v in data.items() if v [3] 10}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using original keys without uppercase conversion.
Using wrong comparison operator like '<'.
Mixing keys and values in the dictionary comprehension.