Bird
0
0

What will be the output of this Python code?

medium📝 Analysis Q5 of 15
LLD - Design — Splitwise (Expense Sharing)
What will be the output of this Python code?
transactions = [{'id': 't1', 'amount': 100}, {'id': 't2', 'amount': 200}]
ids = [t['id'] for t in transactions]
print(ids)
AError
B['t1', 't2']
C[100, 200]
D['amount', 'amount']
Step-by-Step Solution
Solution:
  1. Step 1: Understand list comprehension extracting 'id'

    It creates a list of the 'id' values from each transaction dictionary.
  2. Step 2: Extract ids from given transactions

    Ids are 't1' and 't2', so the list is ['t1', 't2'].
  3. Final Answer:

    ['t1', 't2'] -> Option B
  4. Quick Check:

    List comprehension extracting ids = ['t1', 't2'] [OK]
Quick Trick: List comprehension extracts values by key [OK]
Common Mistakes:
  • Extracting wrong keys
  • Expecting amounts instead of ids
  • Syntax errors in comprehension

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes