Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove key from dictionary in Python returning new dictionary

I have a dictionary

d = {'a':1, 'b':2, 'c':3}

I need to remove a key, say c and return the dictionary without that key in one function call

{'a':1, 'b':2}

d.pop('c') will return the key value - 3 - instead of the dictionary.

I am going to need one function solution if it exists, as this will go into comprehensions

like image 742
Xeos Avatar asked Jul 15 '13 23:07

Xeos


People also ask

Can I remove key from dictionary Python?

To remove a key from a dictionary in Python, use the pop() method or the “del” keyword. Both methods work the same in that they remove keys from a dictionary. The pop() method accepts a key name as argument whereas “del” accepts a dictionary item after the del keyword.

How do you delete the key and return the dictionary from the function?

Python dictionary pop() is a built-in method that removes a key from the dictionary. The pop() method accepts a key argument and deletes that key-value element. For example, the dict pop() method removes and returns an item from a dictionary provided the given key.

What is the wrong way of deleting the value of key key from a dictionary in Python?

If you call pop() on a key that doesn't exist, Python would return a KeyError . So only use pop(key) if you're confident that the key exists in the dictionary. If you are unsure if the key exists then put a value for the second, optional argument for pop() - the default value.

Which would you use to delete an existing key value pair from a dictionary?

To delete a key, value pair in a dictionary, you can use the del method. A disadvantage is that it gives KeyError if you try to delete a nonexistent key. So, instead of the del statement you can use the pop method.


4 Answers

How about this:

{i:d[i] for i in d if i!='c'}

It's called Dictionary Comprehensions and it's available since Python 2.7.

or if you are using Python older than 2.7:

dict((i,d[i]) for i in d if i!='c')
like image 196
jh314 Avatar answered Oct 02 '22 12:10

jh314


Why not roll your own? This will likely be faster than creating a new one using dictionary comprehensions:

def without(d, key):
    new_d = d.copy()
    new_d.pop(key)
    return new_d
like image 42
Gustav Larsson Avatar answered Oct 02 '22 12:10

Gustav Larsson


If you need an expression that does this (so you can use it in a lambda or comprehension) then you can use this little hack trick: create a tuple with the dictionary and the popped element, and then get the original item back out of the tuple:

(foo, foo.pop(x))[0]

For example:

ds = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}]
[(d, d.pop('c'))[0] for d in ds]
assert ds == [{'a': 1, 'b': 2}, {'a': 4, 'b': 5}]

Note that this actually modifies the original dictionary, so despite being a comprehension, it's not purely functional.

like image 39
z0r Avatar answered Oct 02 '22 13:10

z0r


When you invoke pop the original dictionary is modified in place.

You can return that one from your function.

>>> a = {'foo': 1, 'bar': 2}
>>> a.pop('foo')
1
>>> a
{'bar': 2}
like image 36
Felix Yuan Avatar answered Oct 02 '22 12:10

Felix Yuan