In Scala, to remove one key from a dictionary I need to do (pasted from REPL):
scala> Map(9 -> 11, 7 -> 6, 89 -> 43) - 9
res4: scala.collection.immutable.Map[Int,Int] = Map(7 -> 6, 89 -> 43)
To remove multiple keys:
scala> Map(9 -> 11, 7 -> 6, 89 -> 43) -- Seq(9, 89)
res5: scala.collection.immutable.Map[Int,Int] = Map(7 -> 6)
What is the Python way of doing this? (I posted Scala examples because that’s the background I come from.)
If
dis your dictionary andkthe key you want to remove:For example:
If you want to remove multiple:
If you want to do this non-destructively, and get a new dictionary that is a subset, your best bet is:
You could use
k not in lstinstead of dealing withset(lst), but usingsetwill be faster if the list of items to remove is long.