Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a new dictionary, removing some keys and without changing the original

What's the quickest way to make a new dictionary (minus one key) out of an old one without changing the original?

e.g.

input= {potato : 10, book2 : 182, book17 : 12}

make a new dict without book2 without changing the original input

returning = {potato : 10, book17 : 12}

like image 311
Michael Avatar asked Oct 25 '25 00:10

Michael


1 Answers

copy first

d_returning = dict(d_input)

Then delete

del d_returning['book2']
like image 74
Gang Avatar answered Oct 26 '25 18:10

Gang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!