``````

In [1]:

d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}

``````
``````

In [2]:

value = d['key1']
print(value)

``````
``````

aaa

``````
``````

In [3]:

keys = [k for k, v in d.items() if v == 'aaa']
print(keys)

``````
``````

['key1', 'key2']

``````
``````

In [4]:

keys = [k for k, v in d.items() if v == 'bbb']
print(keys)

``````
``````

['key3']

``````
``````

In [5]:

keys = [k for k, v in d.items() if v == 'xxx']
print(keys)

``````
``````

[]

``````
``````

In [6]:

key = [k for k, v in d.items() if v == 'aaa'][0]
print(key)

``````
``````

key1

``````
``````

In [7]:

key = [k for k, v in d.items() if v == 'bbb'][0]
print(key)

``````
``````

key3

``````
``````

In [8]:

# key = [k for k, v in d.items() if v == 'xxx'][0]
# print(key)
# IndexError: list index out of range

``````
``````

In [9]:

def get_keys_from_value(d, val):
return [k for k, v in d.items() if v == val]

``````
``````

In [10]:

keys = get_keys_from_value(d, 'aaa')
print(keys)

``````
``````

['key1', 'key2']

``````
``````

In [11]:

def get_key_from_value(d, val):
keys = [k for k, v in d.items() if v == val]
if keys:
return keys[0]
return None

``````
``````

In [12]:

key = get_key_from_value(d, 'aaa')
print(key)

``````
``````

key1

``````
``````

In [13]:

key = get_key_from_value(d, 'bbb')
print(key)

``````
``````

key3

``````
``````

In [14]:

key = get_key_from_value(d, 'xxx')
print(key)

``````
``````

None

``````
``````

In [15]:

d_num = {'key1': 1, 'key2': 2, 'key3': 3}

``````
``````

In [16]:

keys = [k for k, v in d_num.items() if v >= 2]
print(keys)

``````
``````

['key2', 'key3']

``````
``````

In [17]:

keys = [k for k, v in d_num.items() if v % 2 == 1]
print(keys)

``````
``````

['key1', 'key3']

``````
``````

In [18]:

d_str = {'key1': 'aaa@xxx.com', 'key2': 'bbb@yyy.net', 'key3': 'ccc@zzz.com'}

``````
``````

In [19]:

keys = [k for k, v in d_str.items() if v.endswith('com')]
print(keys)

``````
``````

['key1', 'key3']

``````