Home > Enterprise >  How to add next value of a key to the above value of another key in dictionary
How to add next value of a key to the above value of another key in dictionary

Time:11-01

I have a dictionary inside a list(kind of a json format):

inputt=[{'i':[{'start_len': 261},{'start_len': 450},{'start_len': 172},{'start_len':17}]},
        {'i':[{'start_len': 484}, {'start_len':20},{'start_len': 0 }]}]

Now I want to add a new key end_len inside a value of list that is the value to key i and add the next value of start_len to the new key created like below:-

output=[{'i':[{'start_len': 261,'end_len':450},{'start_len': 450,'end_len':172},{'start_len': 172,'end_len':17},{'start_len':17,'end_len':17}]},
        {'i':[{'start_len': 484,'end_len':20}, {'start_len':20,'end_len':0},{'start_len': 0,'end_len': 0}]}]

I have tried to do it using enumerate but the it is adding index value to it but not the next value which it it appending. Below is the code that I tried.

for i in inputt:
    for j,k in i.items():
        for o in k:
            for K in list(o.keys()):
                o['end_len']=0
            for index,(K,val) in enumerate(o.items()):
                o['end_len']= index val
            print(o)

Output that I got:-

{'start_len': 261, 'end_len': 262}
{'start_len': 450, 'end_len': 451}
{'start_len': 172, 'end_len': 173}
{'start_len': 17, 'end_len': 18}
{'start_len': 484, 'end_len': 485}
{'start_len': 20, 'end_len': 21}
{'start_len': 0, 'end_len': 1}

Excepted Output:-

output=[{'i':[{'start_len': 261,'end_len':450},{'start_len': 450,'end_len':172},{'start_len': 172,'end_len':17},{'start_len':17,'end_len':17}]},
        {'i':[{'start_len': 484,'end_len':20}, {'start_len':20,'end_len':0},{'start_len': 0,'end_len': 0}]}]

Is there any better way of solving this?

CodePudding user response:

One approach using itertools.zip_longest and itertools.tee to iterate pairwise:

import pprint
from itertools import zip_longest, tee

inputt = [{'i': [{'start_len': 261}, {'start_len': 450}, {'start_len': 172}, {'start_len': 17}]},
          {'i': [{'start_len': 484}, {'start_len': 20}, {'start_len': 0}]}]

for dicc in inputt:
    a, b = tee(dicc["i"], 2)
    next(b)  # move the second one place forward
    last_value = dicc["i"][-1]
    for first, second in zip_longest(a, b, fillvalue=last_value):
        first["end_len"] = second["start_len"]

pprint.pprint(inputt)

Output

[{'i': [{'end_len': 450, 'start_len': 261},
        {'end_len': 172, 'start_len': 450},
        {'end_len': 17, 'start_len': 172},
        {'end_len': 17, 'start_len': 17}]},
 {'i': [{'end_len': 20, 'start_len': 484},
        {'end_len': 0, 'start_len': 20},
        {'end_len': 0, 'start_len': 0}]}]

CodePudding user response:

If I understand correctly what you need, you can use a simple nested loop as:

from copy import deepcopy

inputt=[{'i':[{'start_len': 261},{'start_len': 450},{'start_len': 172},{'start_len':17}]},
        {'i':[{'start_len': 484}, {'start_len':20},{'start_len': 0 }]}]
inputt

output = deepcopy(inputt)
for li in output:
    for j in range(len(li["i"])):
        # The elememnt at position `j`
        lj = li["i"][j]
        # The element at j 1 if not at the end of the list
        ljp1 = li["i"][j 1] if j < len(li["i"])-1 else li["i"][j]
        # Set end_len to the next start_len
        lj["end_len"] = ljp1["start_len"]
print(output)

Result

[{'i': [{'start_len': 261, 'end_len': 450},
   {'start_len': 450, 'end_len': 172},
   {'start_len': 172, 'end_len': 17},
   {'start_len': 17, 'end_len': 17}]},
 {'i': [{'start_len': 484, 'end_len': 20},
   {'start_len': 20, 'end_len': 0},
   {'start_len': 0, 'end_len': 0}]}]
  • Related