-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
  • 10,520
  • 2
  • 39
  • 66
Valakatla Ashok
  • 279
  • 1
  • 2
  • 12

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
  • 18,048
  • 6
  • 31
  • 42