Bird
0
0

You have a list of lists: data = [[1, 2], [3, 4], [5, 6]]. How do you print all numbers one by one using iteration?

hard📝 Application Q15 of 15
Python - For Loop
You have a list of lists: data = [[1, 2], [3, 4], [5, 6]]. How do you print all numbers one by one using iteration?
Aprint(data[0][0], data[1][1], data[2][2])
Bfor sublist in data: for num in sublist: print(num)
Cfor i in range(len(data)): print(data[i])
Dfor num in data: print(num)
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested lists and iteration

    Each item in data is a list itself, so we need two loops: one for each sublist, one for numbers inside.
  2. Step 2: Use nested for loops to access all numbers

    The outer loop goes through each sublist, the inner loop prints each number inside that sublist.
  3. Final Answer:

    for sublist in data: for num in sublist: print(num) -> Option B
  4. Quick Check:

    Nested loops print all inner items [OK]
Quick Trick: Use nested loops for lists inside lists [OK]
Common Mistakes:
MISTAKES
  • Trying to print sublists directly
  • Using single loop only
  • Accessing wrong indexes causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes