Search code examples
pythondictionarydel

How to delete a value from a dictionary?


How can I delete a value, for example John, out of the dictionary?

data = {'Player': ['John','Steffen'], age: [25,26]} 
data.pop("Player","John")

I just can’t find how I can delete a key like Player.


Solution

  • I believe you are trying to remove "John" from the list stored in the dictionary. You first have to get a reference to that list, this can be done with the "Player" key. Then you can use the remove method of the list class to remove an item from it.

    An example I think solves your problem is

    data = {'Player': ['John','Steffen'], age: [25,26]} 
    data['Player'].remove('John') # First reference the list, then remove an item from it
    

    I'd take a guess and say the list of ages corresponds to the list of players, so you'd have to also remove John's age from there.