2

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:

  1. 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 pair

  2. newdict={dict.items()[0]} - TypeError: 'dict_items' object is not subscriptable

  3. newdict={dict.keys()[0],dict.pop(dict.keys()[0])} - TypeError: 'dict_keys' object is not subscriptable

  4. key=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.

Kalev Maricq
  • 617
  • 1
  • 7
  • 24
  • You can use `next(iter(old_dict))` to get the key of a single element and use it to remove from `old_dict` and add to `new_dict`. – Raj Dec 17 '19 at 14:46
  • @Georgy Yes, as it turns out it does, though I had previously glanced at that question and thought that it would only help access, not remove. Izaak's answer below also answers my question. – Kalev Maricq Dec 17 '19 at 15:06
  • I went ahead and voted to close, though I do think that others looking to pop/remove instead of access might also miss the answer in the other question. – Kalev Maricq Dec 17 '19 at 15:13

3 Answers3

3

I think this is a good use-case for dict.popitem:

old_dict = {1: 2, 3: 4, 5: 6}
new_dict = {7: 8}

def move_item(old, new):
    k, v = old.popitem()
    new[k] = v

move_item(old_dict, new_dict)
print(old_dict, new_dict)

with result

{1: 2, 3: 4} {7: 8, 5: 6}
Izaak van Dongen
  • 2,450
  • 13
  • 23
1

Try:

for key, value in old_dict:
    new_dict[key] = old_dict.pop(key)
    # operate with new_dict
manuhortet
  • 479
  • 7
  • 20
  • 1
    Hi there! I think perhaps you're being a little overzealous with the `=` signs `:)` (just one would do) – Izaak van Dongen Dec 17 '19 at 14:48
  • 2
    My function is basically creating a Venn diagram from an arbitrary number of named sets. Order doesn't matter and the sets should have names (keys), so a dictionary seems like the obvious choice. The way the function operates is by removing one element from the dictionary, computing the venn diagram groups for the (n-1) sets, and intersecting the new set with each. Am I missing something, or is this a situation where popping an element from a dictionary is useful? – Kalev Maricq Dec 17 '19 at 15:00
  • I think you are right, a dictionary seems to be the correct choice. I was trying to answer to the theoretical question of 'how to remove an item from a dict and create a new one with that item'. Knowing what you are doing, I think you can just iterate over the dict items. I will update my response to show you what I mean. – manuhortet Dec 17 '19 at 15:08
0

You can iterate

for key, value in dict.items():
    if value == bla:
        dict.pop(key)
Ramon Medeiros
  • 2,272
  • 2
  • 24
  • 41