Bird
0
0

Consider this Python code snippet to generate bigrams from a list of words:

medium📝 Debug Q14 of 15
NLP - Text Generation
Consider this Python code snippet to generate bigrams from a list of words:
words = ['hello', 'world', 'hello']
bigrams = [(words[i], words[i+1]) for i in range(len(words))]

What error will this code produce?
ANo error, code runs correctly
BSyntaxError: invalid syntax
CTypeError: unsupported operand type(s)
DIndexError: list index out of range
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loop range

    The loop runs from 0 to len(words)-1, which is 0 to 2 for 3 words.
  2. Step 2: Check index access inside loop

    At i=2, words[i+1] tries to access words[3], which is out of range, causing IndexError.
  3. Final Answer:

    IndexError: list index out of range -> Option D
  4. Quick Check:

    Loop index exceeds list length = D [OK]
Quick Trick: Check loop range when accessing i+1 index [OK]
Common Mistakes:
MISTAKES
  • Using full length in range causing out-of-bounds
  • Assuming no error without testing
  • Confusing syntax errors with runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes