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.
Asked
Active
Viewed 553 times
0
-
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](https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists/11520540). Start with that and see if it helps. – Code-Apprentice May 28 '21 at 20:53
-
4Please provide the exact code that isn't working for you, because on my computer this works fine. – Kraigolas May 28 '21 at 20:54
-
If you need additional help, please post a [mcve] 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 May 28 '21 at 20:55
1 Answers
3
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']}

blackraven
- 5,284
- 7
- 19
- 45