Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Mujeeb urpxt




Posted Questions



Wait...

Posted Answers



Answer


  • Security manager.
  • Forensic accountant.
  • Police sergeant.
  • Judge.
  • Forensic analyst.
  • Lawyer.
  • Detective.
  • Border patrol agent.

Answer is posted for the following question.

How to know best police jobs in Alaska?

Answer


1
# Insert dictionary item into a dictionary at specified position: 
2
def insert_item(dic, item={}, pos=None):
3
    """
4
    Insert a key, value pair into an ordered dictionary.
5
    Insert before the specified position.
6
    """
7
    from collections import OrderedDict
8
    d = OrderedDict()
9
    # abort early if not a dictionary:
10
    if not item or not isinstance(item, dict):
11
        print('Aborting. Argument item must be a dictionary.')
12
        return dic
13
    # insert anywhere if argument pos not given: 
14
    if not pos:
15
        dic.update(item)
16
        return dic
17
    for item_k, item_v in item.items():
18
        for k, v in dic.items():
19
            # insert key at stated position:
20
            if k == pos:
21
                d[item_k] = item_v
22
            d[k] = v
23
    return d
24
25
d = {'A':'letter A', 'C': 'letter C'}
26
insert_item(['A', 'C'], item={'B'})
27
## Aborting. Argument item must be a dictionary.
28
29
insert_item(d, item={'B': 'letter B'})
30
## {'A': 'letter A', 'C': 'letter C', 'B': 'letter B'}
31
32
insert_item(d, pos='C', item={'B': 'letter B'})
33
# OrderedDict([('A', 'letter A'), ('B', 'letter B'), ('C', 'letter C')])

Answer is posted for the following question.

How to python 3.7 insert at place in dict (Python Programing Language)


Wait...