Logo

Programming-Idioms

  • Java
  • Python

Idiom #52 Check if map contains value

Determine whether the map m contains an entry with the value v, for some key.

Is this value contained in this map, for any key?
x = False
for y in m.items():
    if y[1] == v:
        x = y[0]
        break
def k(x, m):
    for k, v in m.items():
        if v == x: return k
k = k(v, m)
v in m.values()
import java.util.Map;
m.containsValue(v)
(boolean (some #{v} (vals m)))

In Clojure values are truthy/falsy

no need for boolean, however included due to other language examples

New implementation...