0
0
Pythonprogramming~10 mins

enumerate() 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 use enumerate to get index and value from the list.

Python
fruits = ['apple', 'banana', 'cherry']
for [1], fruit in enumerate(fruits):
    print(f"[1]: {fruit}")
Drag options to blanks, or click blank then click option'
Aindex
Bi
Ccount
Dnum
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the wrong variable name that is not assigned by enumerate.
Forgetting that enumerate returns two values.
2fill in blank
medium

Complete the code to start enumerate counting from 1 instead of 0.

Python
colors = ['red', 'green', 'blue']
for idx, color in enumerate(colors, [1]):
    print(f"{idx}: {color}")
Drag options to blanks, or click blank then click option'
A0
B2
C1
D-1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing 0 which is the default and does not change the start.
Passing a negative number which is unusual for indexing.
3fill in blank
hard

Fix the error in the code to correctly unpack the values from enumerate.

Python
items = ['pen', 'pencil', 'eraser']
for [1] in enumerate(items):
    print(f"{index}: {item}")
Drag options to blanks, or click blank then click option'
Aidx, val
Bitem
Ci
Dindex, item
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using only one variable to unpack two values.
Using variable names that don't match in the print statement.
4fill in blank
hard

Fill both blanks to create a dictionary with index as key and item as value using enumerate.

Python
items = ['a', 'b', 'c']
indexed = [1](items)
result = {idx: [2] for idx, val in indexed}
Drag options to blanks, or click blank then click option'
Aenumerate
Bval
Cidx
Dlist
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using list instead of enumerate.
Using the index as the dictionary value instead of the item.
5fill in blank
hard

Fill all three blanks to create a list of strings showing index and item using enumerate.

Python
words = ['dog', 'cat', 'bird']
result = [f"[1]: [2]" for [3], word in enumerate(words)]
Drag options to blanks, or click blank then click option'
Ai
Bword
Cindex
Dj
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different variable names inconsistently.
Using the wrong variable for index or item.