Facing trouble when slicing dictionaries in a list

I have a list of dictionary in Python as below:

List = [
    {
        'id': 0,
        'value': 10
    },
    {
        'id': 1,
        'value': 20
    },
    {
        'id': 2,
        'value': 80
    },
    {
        'id': 3,
        'value': 115
    },
    {
        'id': 4,
        'value': 130
    },
    {
        'id': 5,
        'value':135
    },
    {
        'id': 6,
        'value':250
    },
    {
        'id': 7,
        'value':280
    }
]

The final output requirement is to check the difference between the value from the first dictionary and other values from the subsequent dictionaries whereby the difference must be <=50, in order to be categorized in the same list (forming a new list).
The expected output is as shown below.

[
    [
        {
            'id': 0,
            'value': 10
        },
        {
            'id': 1,
            'value': 20
        }
    ],
    [
        {
            'id': 2,
            'value': 80
        },
        {
            'id': 3,
            'value': 115
        },
        {
            'id': 4,
            'value': 130
        },
    ],
    [
        {
            'id': 5,
            'value':135
        }
    ],
    [
        {
            'id': 6,
            'value':250
        },
        {
            'id': 7,
            'value':280
        }
    ]
]

I hope someone can help with this! Thank you.

Have you made an attempt yourself?

Could use some more information. Are the "id" values guaranteed to increase by 1, starting from 0? Are the "value" values guaranteed to increase monotonically? If not, how are negative differences treated?

1 Like

It doesn’t help that you spread your data out so widely that it doesn’t fit on the screen all at once. Making it a little bit more compact so your readers can actually see it helps.

You can try this. Does this help? (I haven’t run it to see if it does what you want, because I don’t really understand what you want.)

# Much easier to read!
inlist = [{'id': 0, 'value': 10},
          {'id': 1, 'value': 20},
          {'id': 2, 'value': 80},
          {'id': 3, 'value': 115},
          {'id': 4, 'value': 130},
          {'id': 5, 'value': 135},
          {'id': 6, 'value': 250},
          {'id': 7, 'value': 280},
          ]


outlist = []
for index, mapping in enumerate(inlist):
    alist = [mapping]
    for d in inlist[index+1:]:
        if abs(mapping['value'] - d['value']) <= 50:
            alist.append(d)
    if alist:
        outlist.append(alist)
1 Like

This should produce the desired output.

inlist = [
    {"id": 0, "value": 10},
    {"id": 1, "value": 20},
    {"id": 2, "value": 80},
    {"id": 3, "value": 115},
    {"id": 4, "value": 130},
    {"id": 5, "value": 135},
    {"id": 6, "value": 250},
    {"id": 7, "value": 280},
]
outlist = []
for d in inlist:
    if not outlist:
        outlist.append([d])
    elif d["value"] - outlist[-1][-1]["value"] <= 50:
        outlist[-1].append(d)
    else:
        outlist.append([d])
1 Like

Have you made an attempt yourself?

  • Yes, but I couldn’t get the desired output

Are the "id" values guaranteed to increase by 1, starting from 0?

  • Yes

Are the "value" values guaranteed to increase monotonically?

  • Yes, so there won’t be any negative differences

Thanks for the tips! I’m new to this forum, may I know if I am only allowed to edit my post once? as currently I can’t seem to have any option to edit my post.

Regarding the code, I have tried but the output is not the desired output.

[
[{'id': 0, 'value': 10}, {'id': 1, 'value': 20}],
[{'id': 1, 'value': 20}],
[{'id': 2, 'value': 80}, {'id': 3, 'value': 115}, {'id': 4, 'value': 130}],
[{'id': 3, 'value': 115}, {'id': 4, 'value': 130}, {'id': 5, 'value': 135}],
[{'id': 4, 'value': 130}, {'id': 5, 'value': 135}],
[{'id': 5, 'value': 135}],
[{'id': 6, 'value': 250}, {'id': 7, 'value': 280}],
[{'id': 7, 'value': 280}]
]

The output:

[
[{'id': 0, 'value': 10}, {'id': 1, 'value': 20}],
[{'id': 2, 'value': 80}, {'id': 3, 'value': 115}, {'id': 4, 'value': 130}, {'id': 5, 'value': 135}],
[{'id': 6, 'value': 250}, {'id': 7, 'value': 280}]
]

However, it should be:

[
[{'id': 0, 'value': 10}, {'id': 1, 'value': 20}],
[{'id': 2, 'value': 80}, {'id': 3, 'value': 115}, {'id': 4, 'value': 130}],
[{'id': 5, 'value': 135}],
[{'id': 6, 'value': 250}, {'id': 7, 'value': 280}]
]

Note that the dictionary of {‘id’: 5, ‘value’: 135} should be forming a new list instead in a same list with {‘id’: 2, ‘value’: 80}, because 135 - 80 = 55 which is more than 50 already.

PS: In order to be in the same list, the differences of the values (from subsequent dictionaries) and the first dictionary in a new list (using the example above, it is {‘id’: 2, ‘value’: 80}) must be less than or equal to 50 only.

Ah, the criterion applies to elements within the same sublist. I understood it as applying to elements in consecutive sublists. Then, this should do it:

inlist = [
    {"id": 0, "value": 10},
    {"id": 1, "value": 20},
    {"id": 2, "value": 80},
    {"id": 3, "value": 115},
    {"id": 4, "value": 130},
    {"id": 5, "value": 135},
    {"id": 6, "value": 250},
    {"id": 7, "value": 280},
]
outlist = []
for d in inlist:
    if not outlist:
        outlist.append([d])
    elif d["value"] - outlist[-1][0]["value"] <= 50:
        outlist[-1].append(d)
    else:
        outlist.append([d])
1 Like

It works! Thank you :slight_smile: