0
0
Pythonprogramming~5 mins

enumerate() function in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the enumerate() function do in Python?
It adds a counter to an iterable (like a list) and returns it as an enumerate object, which yields pairs of (index, item).
Click to reveal answer
beginner
How do you use enumerate() with a list fruits = ['apple', 'banana', 'cherry'] to get index and fruit?
Use a for loop:
for index, fruit in enumerate(fruits):
print(index, fruit)
Click to reveal answer
intermediate
What is the purpose of the optional start parameter in enumerate()?
It sets the starting index number. By default, it starts at 0, but you can change it to any integer.
Click to reveal answer
intermediate
What type of object does enumerate() return?
It returns an enumerate object, which is an iterator that produces pairs of (index, item) when looped over.
Click to reveal answer
beginner
Why is enumerate() better than using a manual counter variable in a loop?
Because it is cleaner, less error-prone, and makes the code easier to read by automatically handling the index counting.
Click to reveal answer
What does enumerate(['a', 'b', 'c']) produce when converted to a list?
A[(1, 'a'), (2, 'b'), (3, 'c')]
B['a', 'b', 'c']
C[(0, 'a'), (1, 'b'), (2, 'c')]
D['0a', '1b', '2c']
How can you start enumeration at 1 instead of 0?
Aenumerate(iterable, start=1)
Benumerate(iterable, 1)
Cenumerate(iterable).start(1)
Denumerate(iterable).index(1)
Which of these is a correct way to get index and value from a list nums?
Afor i in enumerate(nums): val = i
Bfor i, val in enumerate(nums):
Cfor val, i in enumerate(nums):
Dfor i in nums.enumerate():
What type of object is returned by enumerate()?
AEnumerate object (iterator)
BTuple
CDictionary
DList
Why might you prefer enumerate() over a manual counter variable?
AIt automatically handles counting and is less error-prone.
BIt runs faster than any loop.
CIt creates a dictionary automatically.
DIt sorts the list while looping.
Explain how the enumerate() function works and give an example of its use.
Think about how you get both position and value when looping.
You got /3 concepts.
    Describe the purpose of the start parameter in enumerate() and how to use it.
    It lets you choose where counting begins.
    You got /3 concepts.