-2

My data is a dictionary with a list of dictionaries inside.

original_dictionary = {
    'graphid': '122230',
    'items': [
        {
            'itemid': '23981'
        },
        {
            'itemid': '23982'
        },
        {
            'itemid': '23983'
        }
    ]
}

What I am trying to do is to have a new dictionary as mentioned below

need_dictionary = {'graphid': '122230', 'items': ['23981','23982','23983']}

I tried dictionary.keys(), dictionary.values() does not help.

NutCracker
  • 11,485
  • 4
  • 44
  • 68
Ashok Developer
  • 331
  • 1
  • 2
  • 16

1 Answers1

0

You can make a copy of the original & then pull out the required details from the original like so:

original_dictionary = {'graphid': '122230', 'items': [{'itemid': '23981'}, {'itemid': '23982'}, {'itemid': '23983'}]}

need_dictionary = original_dictionary.copy()
need_dictionary['items'] = [x['itemid'] for x in original_dictionary['items']]

print(need_dictionary)

Output:

{'graphid': '122230', 'items': ['23981', '23982', '23983']}
rdas
  • 20,604
  • 6
  • 33
  • 46