Bird
0
0

You have a list of reviews with aspects and sentiments:

hard📝 Application Q8 of 15
NLP - Sentiment Analysis Advanced
You have a list of reviews with aspects and sentiments:
reviews = [
  {"aspect": "battery", "sentiment": "positive"},
  {"aspect": "screen", "sentiment": "negative"},
  {"aspect": "battery", "sentiment": "neutral"}
]

How would you create a dictionary that keeps the last sentiment for each aspect?
AUse a set to store unique sentiments only
BUse a list to store all sentiments without keys
CUse a loop to assign each aspect's sentiment, overwriting previous
DUse a tuple to store aspect and sentiment pairs
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal to keep last sentiment per aspect

    Assigning each aspect's sentiment in a dictionary overwrites previous values, keeping the last.
  2. Step 2: Evaluate options for data structure use

    Looping and assigning to a dictionary key is the correct way. Lists, sets, or tuples do not overwrite by key.
  3. Final Answer:

    Use a loop to assign each aspect's sentiment, overwriting previous -> Option C
  4. Quick Check:

    Dictionary assignment overwrites previous values [OK]
Quick Trick: Dictionary keys overwrite values on repeated assignment [OK]
Common Mistakes:
MISTAKES
  • Using list or set which don't map keys to values
  • Expecting sets to keep order or overwrite
  • Using tuples which are immutable and unordered

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes