Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
Python - Structured Data Files
What will be the output of this code snippet?
import csv
rows = [['id', 'score'], ['1', '85'], ['2', '90']]
with open('scores.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(rows)

with open('scores.csv', 'r') as f:
    reader = csv.reader(f)
    data = list(reader)
print(data)
Aid,score\n1,85\n2,90
B[['id', 'score'], ['1', '85'], ['2', '90']]
C[('id', 'score'), ('1', '85'), ('2', '90')]
DError: writerows() requires a dictionary
Step-by-Step Solution
Solution:
  1. Step 1: Writing multiple rows with writerows()

    The writerows() method writes each list inside rows as a CSV row.
  2. Step 2: Reading all rows into a list

    Reading back with csv.reader and converting to list returns a list of lists representing rows.
  3. Final Answer:

    [['id', 'score'], ['1', '85'], ['2', '90']] -> Option B
  4. Quick Check:

    writerows() writes multiple rows; reading returns list of lists [OK]
Quick Trick: writerows() writes multiple rows from a list of lists [OK]
Common Mistakes:
  • Expecting string output instead of list
  • Confusing tuples with lists in output
  • Thinking writerows() needs dictionaries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes