Python key value map

By nature, a dictionary is a one-to-one mapping, but it’s not hard to make it one-to-many—in other words, to make one key map to multiple values. There are two possible approaches, depending on how you want to treat duplications in the set of values for a key. The following approach allows such duplications:

d1 = {}
d1.setdefault(key, []).append(value)

while this approach automatically eliminates duplications:

d2 = {}
d2.setdefault(key, {})[value] = 1

A normal dictionary performs a simple mapping of a key to a value. This recipe shows two easy, efficient ways to achieve a mapping of each key to multiple values. The semantics of the two approaches differ slightly but importantly in how they deal with duplication. Each approach relies on the

d2 = {}
d2.setdefault(key, {})[value] = 1
7 method of a dictionary to initialize the entry for a key in the dictionary, if needed, and in any case to return said entry.

Of course, you need to be able to do more than just add values for a key. With the first approach, which allows duplications, here’s how to retrieve the list of values for a key:

list_of_values = d1[key]

Here’s how to remove one value for a key, if you don’t mind leaving empty lists as items of

d2 = {}
d2.setdefault(key, {})[value] = 1
8 when the last value for a key is removed:

d1[key].remove(value)

Despite the empty lists, it’s still easy to test for the existence of a key with at least one value:

def has_key_with_some_values(d, key):
    return d.has_key(key) and d[key]

This returns either

d2 = {}
d2.setdefault(key, {})[value] = 1
9 or a list, which may be empty. In most cases, it is easier to use a function that always returns a list (maybe an empty one), such as:

def get_values_if_any(d, key):
    return d.get(key, [])

You can use either of these functions in a statement. For example:

if get_values_if_any(d1, somekey):
if has_key_with_some_values(d1, somekey):

However,

list_of_values = d1[key]
0 is generally handier. For example, you can use it to check if
list_of_values = d1[key]
1 is among the values for
list_of_values = d1[key]
2:

if 'freep' in get_values_if_any(d1, somekey):

This extra handiness comes from

list_of_values = d1[key]
0 always returning a list, rather than sometimes a list and sometimes
d2 = {}
d2.setdefault(key, {})[value] = 1
9.

The first approach allows each value to be present multiple times for each given key. For example:

example = {}
example.setdefault('a', []).append('apple')
example.setdefault('b', []).append('boots')
example.setdefault('c', []).append('cat')
example.setdefault('a', []).append('ant')
example.setdefault('a', []).append('apple')

Now

list_of_values = d1[key]
5 is
list_of_values = d1[key]
6. If we now execute:

example['a'].remove('apple')

the following test is still satisfied:

d2 = {}
d2.setdefault(key, {})[value] = 1
0

list_of_values = d1[key]
7 was present twice, and we removed it only once. (Testing for
list_of_values = d1[key]
7 with
list_of_values = d1[key]
9 would be more general, although equivalent in this case.)

The second approach, which eliminates duplications, requires rather similar idioms. Here’s how to retrieve the list of the values for a key:

d2 = {}
d2.setdefault(key, {})[value] = 1
1

Here’s how to remove a key/value pair, leaving empty dictionaries as items of

d1[key].remove(value)
0 when the last value for a key is removed:

d2 = {}
d2.setdefault(key, {})[value] = 1
2

The

d1[key].remove(value)
1 function shown earlier also works for the second approach, and you also have analogous alternatives, such as:

d2 = {}
d2.setdefault(key, {})[value] = 1
3

The second approach doesn’t allow duplication. For example:

d2 = {}
d2.setdefault(key, {})[value] = 1
4

Now

list_of_values = d1[key]
5 is
d1[key].remove(value)
3. Now, if we execute:

d2 = {}
d2.setdefault(key, {})[value] = 1
5

the following test is not satisfied:

d2 = {}
d2.setdefault(key, {})[value] = 1
0

list_of_values = d1[key]
7 was present, but we just removed it.

This recipe focuses on how to code the raw functionality, but if you want to use this functionality in a systematic way, you’ll want to wrap it up in a class. For that purpose, you need to make some of the design decisions that the recipe highlights. Do you want a value to be in the entry for a key multiple times? (Is the entry a bag rather than a set, in mathematical terms?) If so, should

d1[key].remove(value)
5 just reduce the number of occurrences by 1, or should it wipe out all of them? This is just the beginning of the choices you have to make, and the right choices depend on the specifics of your application.

How do you map a key to a value in Python?

Method #1 : Using dictionary comprehension This is one of the way in which we can solve this problem. In this, we iterate the list keys and construct dictionary of required key-value pairs using dictionary comprehension.

How to store key

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. While using Dictionary, sometimes, we need to add or modify the key/value inside the dictionary.

How to check key

Using Keys() The keys() function and the "in" operator can be used to see if a key exists in a dictionary. The keys() method returns a list of keys in the dictionary, and the "if, in" statement checks whether the provided key is in the list. It returns True if the key exists; otherwise, it returns False.

How to iterate key

To iterate over key-value pairs, use the following:.
for k,v in dict. iteritems() in Python 2..
for k,v in dict. items() in Python 3..