That's really just my question. All I can find online is how to remove a key-value pair, but not how to remove a specific value from a key if the values of that key are a list. ex: a key Emotions values are ['neutral', 'happy'] and I only want to remove 'happy'. dictionary[Emotions].remove('happy') isn't working at all.
-
1I suggest you break this down into simpler pieces. First, you need to figure out how to remove an element from a list. Don't worry about the dictionary part. When you google, "python remove element from list", one of the hits is this question on Stack Overflow. Start with that and see if it helps.Code-Apprentice– Code-Apprentice2021-05-28 20:53:52 +00:00Commented May 28, 2021 at 20:53
-
4Please provide the exact code that isn't working for you, because on my computer this works fine.Kraigolas– Kraigolas2021-05-28 20:54:50 +00:00Commented May 28, 2021 at 20:54
-
If you need additional help, please post a minimal reproducible example that shows exaclty what you tried. Then explain exactly what happens when you run your code and what you want it to do differently. BE SPECIFIC. The more detail you provide, the better we can help you. Just saying it "isn't working" doesn't give us enough information.Code-Apprentice– Code-Apprentice2021-05-28 20:55:29 +00:00Commented May 28, 2021 at 20:55
Add a comment
|
1 Answer
I am not sure where the encountered problems are. You may like to compare your codes with the correctly executed codes below:
Emotions = 'emotions'
dictionary = {Emotions: ['neutral', 'happy']}
print(dictionary)
dictionary[Emotions].remove('happy')
print(dictionary)
Output
{'emotions': ['neutral', 'happy']}
{'emotions': ['neutral']}