• ok, I feel stupid, but there must be a better way than this! (finding n

    From Dino@21:1/5 to All on Fri Jan 20 10:29:26 2023
    let's say I have this list of nested dicts:

    [
    { "some_key": {'a':1, 'b':2}},
    { "some_other_key": {'a':3, 'b':4}}
    ]

    I need to turn this into:

    [
    { "value": "some_key", 'a':1, 'b':2},
    { "value": "some_other_key", 'a':3, 'b':4}
    ]

    I actually did it with:

    listOfDescriptors = list()
    for cd in origListOfDescriptors:
    cn = list(cd.keys())[0] # There must be a better way than this!
    listOfDescriptors.append({
    "value": cn,
    "type": cd[cn]["a"],
    "description": cd[cn]["b"]
    })

    and it works, but I look at this and think that there must be a better
    way. Am I missing something obvious?

    PS: Screw OpenAPI!

    Dino

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tobiah@21:1/5 to Dino on Fri Jan 20 08:06:54 2023
    On 1/20/23 07:29, Dino wrote:

    let's say I have this list of nested dicts:

    [
      { "some_key": {'a':1, 'b':2}},
      { "some_other_key": {'a':3, 'b':4}}
    ]

    I need to turn this into:

    [
      { "value": "some_key", 'a':1, 'b':2},
      { "value": "some_other_key", 'a':3, 'b':4}
    ]

    This doesn't look like the program output you're getting.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Dino on Fri Jan 20 16:41:18 2023
    Dino <dino@no.spam.ar> writes:
    cn = list(cd.keys())[0] # There must be a better way than this!

    d = { 'a': 1 }
    one_and_only = d.popitem()
    print( one_and_only[ 0 ])
    |a

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dino@21:1/5 to Tobiah on Fri Jan 20 11:16:33 2023
    On 1/20/2023 11:06 AM, Tobiah wrote:
    On 1/20/23 07:29, Dino wrote:


    This doesn't look like the program output you're getting.

    you are right that I tweaked the name of fields and variables manually
    (forgot a couple of places, my bad) to illustrate the problem more
    generally, but hopefully you get the spirit.

    "value": cn,
    "a": cd[cn]["a"],
    "b": cd[cn]["b"]

    Anyway, the key point (ooops, a pun) is if there's a more elegant way to
    do this (i.e. get a reference to the unique key in a dict() when the key
    is unknown):

    cn = list(cd.keys())[0] # There must be a better way than this!

    Thanks

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jon Ribbens@21:1/5 to Dino on Fri Jan 20 16:46:03 2023
    On 2023-01-20, Dino <dino@no.spam.ar> wrote:

    let's say I have this list of nested dicts:

    [
    { "some_key": {'a':1, 'b':2}},
    { "some_other_key": {'a':3, 'b':4}}
    ]

    I need to turn this into:

    [
    { "value": "some_key", 'a':1, 'b':2},
    { "value": "some_other_key", 'a':3, 'b':4}
    ]

    [{"value": key, **value} for d in input_data for key, value in d.items()]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Oscar Benjamin@21:1/5 to Dino on Fri Jan 20 17:40:09 2023
    On Fri, 20 Jan 2023 at 17:30, Dino <dino@no.spam.ar> wrote:

    let's say I have this list of nested dicts:

    [
    { "some_key": {'a':1, 'b':2}},
    { "some_other_key": {'a':3, 'b':4}}
    ]

    I need to turn this into:

    [
    { "value": "some_key", 'a':1, 'b':2},
    { "value": "some_other_key", 'a':3, 'b':4}
    ]

    You want both the key and the value so you can use items():

    In [39]: L = [
    ...: { "some_key": {'a':1, 'b':2}},
    ...: { "some_other_key": {'a':3, 'b':4}}
    ...: ]

    In [40]: [{"value": k, **m} for l in L for k, m in l.items()]
    Out[40]:
    [{'value': 'some_key', 'a': 1, 'b': 2},
    {'value': 'some_other_key', 'a': 3, 'b': 4}]

    --
    Oscar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rob Cliffe@21:1/5 to Dino on Fri Jan 20 18:44:17 2023
    On 20/01/2023 15:29, Dino wrote:

    let's say I have this list of nested dicts:

    [
      { "some_key": {'a':1, 'b':2}},
      { "some_other_key": {'a':3, 'b':4}}
    ]

    I need to turn this into:

    [
      { "value": "some_key", 'a':1, 'b':2},
      { "value": "some_other_key", 'a':3, 'b':4}
    ]
    Assuming that I believe the above, rather than the code below, this works:

    listOfDescriptors = [
        { **  (L := list(D.items())[0])[1], **{'value' : L[0] } }
        for D in origListOfDescriptors]

    I believe that from Python 3.9 onwards this can be written more
    concisely as:

    listOfDescriptors = [
        { (L := list(D.items())[0])[1] } | {'value' : L[0] }
        for D in origListOfDescriptors]     # untested

    Best wishes
    Rob Cliffe


    I actually did it with:

    listOfDescriptors = list()
    for cd in origListOfDescriptors:
        cn = list(cd.keys())[0] # There must be a better way than this!
        listOfDescriptors.append({
            "value": cn,
            "type": cd[cn]["a"],
            "description": cd[cn]["b"]
        })

    and it works, but I look at this and think that there must be a better
    way. Am I missing something obvious?

    PS: Screw OpenAPI!

    Dino

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dino@21:1/5 to Dino on Sat Jan 21 09:50:54 2023
    I learned new things today and I thank you all for your responses.

    Please consider yourself thanked individually.

    Dino

    On 1/20/2023 10:29 AM, Dino wrote:

    let's say I have this list of nested dicts:


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)