Search code examples
pythonpandasdictionaryiterable-unpacking

unpacking a dictionary in python


I have a dictionary as follows

    dict = {'Sept close adds': close_adds, 'Sept close deletes': close_deletes, 'Sept Changes': annual_changes, 'June Changes': june_changes}

i want to remove the key and value 'June Changes': june_changes from the above dictionary and have the value (june_changes) as a separate variable to use later on in the code.

I have tried to use the below code but it does not create a new variable with the value i want while maintaining the dictionary excluding june_changes.

keys, values = dict.keys(), dict.values()

can someone please help me?


Solution

  • dict.pop does what you want

    >>> d = {"foo":1, "bar":2}
    >>> bar = d.pop("bar")
    >>> d
    {'foo': 1}
    >>> bar
    2