Bird
Raised Fist0

Which of the following is the correct syntax to append a new transaction record to a list named transactions in Python?

easy🧠 Conceptual Q3 of Q15
LLD - Design — Splitwise (Expense Sharing)
Which of the following is the correct syntax to append a new transaction record to a list named transactions in Python?
Atransactions.insert({'id': 't100', 'amount': 50})
Btransactions.add({'id': 't100', 'amount': 50})
Ctransactions.append({'id': 't100', 'amount': 50})
Dtransactions.push({'id': 't100', 'amount': 50})
Step-by-Step Solution
Solution:
  1. Step 1: Recall Python list methods for adding elements

    Python lists use the append() method to add elements at the end.
  2. Step 2: Verify other options

    add() is for sets, insert() requires an index, push() is not a Python list method.
  3. Final Answer:

    transactions.append({'id': 't100', 'amount': 50}) -> Option C
  4. Quick Check:

    Appending to list = append() method [OK]
Quick Trick: Use append() to add items to Python lists [OK]
Common Mistakes:
MISTAKES
  • Using add() which is for sets
  • Using push() which is not a Python list method
  • Using insert() without index

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes