Issue
I have a following dictionary:
dict1 = {'Diacto Group': {'id': 7547,
'type': 'user',
'name': 'Diacto Group',
'filters': [{'column': 'Core Area',
'values': ['Missiles & Aviation Technology'],
'operator': 'EQUALS',
'not': False}],
'users': [],
'virtualUsers': [],
'groups': [360305499]},
'Diacto People': {'id': 7548,
'type': 'user',
'name': 'Diacto People',
'filters': [{'column': 'Core Area',
'values': ['Aircraft Company', 'Aviation Technology'],
'operator': 'EQUALS',
'not': False}],
'users': [326197441, 1293859642],
'virtualUsers': [],
'groups': []},
}
Basically I want to extract either one of the lists from 'users' or 'groups' if they have list containing atleast one value. I want the final output to look like this:
l1 = [# Extracted list as value from 'group' key from Diacto Group key as users key was blank
# list.
[360305499],
# Extracted list as value from 'users' key from Diacto People key as groups key was
# blank list.
[326197441, 1293859642]
]
List comprehension would be more preferable if possible. Thank you for the efforts and time you put into this.
Solution
The simplest I can think of with comprehension, provided either 'users' or 'groups' is not empty:
[v['users']+v['groups'] for v in dict1.values()]
Answered By - Tranbi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.