I have a dictionary and don't know the keys or values. I want to remove a single element (doesn't matter which) and create a new dictionary containing only that key and value, removing it from the old dictionary in the process.
dict(n items) -> newdict(1 item) and dict(n-1 items)
What I've tried:
newdict=dict.pop()
- Would work perfectly for a list, but for dictionary it 1. requires a key and only returns a value, not a new dictionary or a key, value pairnewdict={dict.items()[0]}
- TypeError: 'dict_items' object is not subscriptablenewdict={dict.keys()[0],dict.pop(dict.keys()[0])}
- TypeError: 'dict_keys' object is not subscriptablekey=list(dict)[0] newdict={key,dict.pop(key)}
- Works (finally), but isn't their a better way than converting the entire dictionary to a list just to grab one key?
Is there a more efficient way to move a single dictionary element into a new dictionary?
Edit: Having the single element as a tuple would also work. I just need both the key and value of the element I remove.