简体   繁体   中英

How to transform a dict array with multiple key values into a dict with single key value?

I have the following input data

options_d = [{'id': 36, 'label': 'Angular'},
 {'id': 37, 'label': 'Java'},
 {'id': 38, 'label': 'PHP'},
 {'id': 39, 'label': 'Python'},
 {'id': 40, 'label': 'C#'},
 {'id': 41, 'label': 'C'},
 {'id': 42, 'label': '.NET'},
 {'id': 43, 'label': 'Ruby'},
 {'id': 44, 'label': 'Rails'},
 {'id': 45, 'label': 'OS-Linux'},
 {'id': 55, 'label': 'Maschinenbau'},
 {'id': 56, 'label': 'Automotive'},
 {'id': 57, 'label': 'Engineering'}]

And I would like to have it transformed into

{36: 'Angular',
 37: 'Java',
 38: 'PHP',
 39: 'Python',
 40: 'C#',
 41: 'C',
 42: '.NET',
 43: 'Ruby',
 44: 'Rails',
 45: 'OS-Linux',
 55: 'Maschinenbau',
 56: 'Automotive',
 57: 'Engineering'}

What I did so far is

skillsmap_person = {}
for option in options_d:
    skillsmap_person[option['id']] = option['label']

It works. However, is there a one-line dict assignment solution I can use?

Any ideas?

Creating dictionary with dict comprehensions -

options_dict = {i['id']:i['label'] for i in options_d}
print(options_dict)
    {36: 'Angular',
     37: 'Java',
     38: 'PHP',
     39: 'Python',
     40: 'C#',
     41: 'C',
     42: '.NET',
     43: 'Ruby',
     44: 'Rails',
     45: 'OS-Linux',
     55: 'Maschinenbau',
     56: 'Automotive',
     57: 'Engineering'}

Why not doing dict with list comprehension:

>>> dict([i.values() for i in options_d])
{36: 'Angular', 37: 'Java', 38: 'PHP', 39: 'Python', 40: 'C#', 41: 'C', 42: '.NET', 43: 'Ruby', 44: 'Rails', 45: 'OS-Linux', 55: 'Maschinenbau', 56: 'Automotive', 57: 'Engineering'}
>>> 

Or for lower versions (when dictionaries are unordered):

>>> dict([list(i.values())[::-1] for i in options_d])
{36: 'Angular', 37: 'Java', 38: 'PHP', 39: 'Python', 40: 'C#', 41: 'C', 42: '.NET', 43: 'Ruby', 44: 'Rails', 45: 'OS-Linux', 55: 'Maschinenbau', 56: 'Automotive', 57: 'Engineering'}
>>> 

It simple iterates through the list of dictionaries, and get's the values of the dictionary, then have an outer dict(...) for making it a dictionary, so actually, odd indexed value would be keys, even indexed value would be values.

options_d = [{'id': 36, 'label': 'Angular'},
 {'id': 37, 'label': 'Java'},
 {'id': 38, 'label': 'PHP'},
 {'id': 39, 'label': 'Python'},
 {'id': 40, 'label': 'C#'},
 {'id': 41, 'label': 'C'},
 {'id': 42, 'label': '.NET'},
 {'id': 43, 'label': 'Ruby'},
 {'id': 44, 'label': 'Rails'},
 {'id': 45, 'label': 'OS-Linux'},
 {'id': 55, 'label': 'Maschinenbau'},
 {'id': 56, 'label': 'Automotive'},
 {'id': 57, 'label': 'Engineering'}]

{elem['id'] : elem['label'] for elem in options_d}

gets you:

{36: 'Angular',
 37: 'Java',
 38: 'PHP',
 39: 'Python',
 40: 'C#',
 41: 'C',
 42: '.NET',
 43: 'Ruby',
 44: 'Rails',
 45: 'OS-Linux',
 55: 'Maschinenbau',
 56: 'Automotive',
 57: 'Engineering'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM