I have this dictionary object
{'owner': {'email': 'email@address'},'users': [{'username': 'FrankT','memberType': 'member'}, {'username': 'Chris','memberType': 'admin'}]}
I want to read it and get the Email from 'owner' key to a single variable and then FOR loop it through the second Key 'users' and get a variable for 'username' - 'memberType'
For instance I might want to read each of the users in the users Key and only write those that have membertype set to Admin.... Populate an array or dict ....
Or simply loop through the users key and make a comma delimited string of the user names
I just dont know how to read and loop through the keys...
Any help would be appreciated.
Solved! Go to Solution.
Do you mind providing an example of what you want to end up with? I want to make sure I'm understanding your needs. This was my understanding of what you're asking for:
data = {'owner': {'email': 'email@address'},'users': [{'username': 'FrankT','memberType': 'member'}, {'username': 'Chris','memberType': 'admin'}, {'username': 'Test','memberType': 'admin'}]}
email = data['owner']['email']
users = data['users']
admins = [{'email': email, **user} for user in users if user['memberType'] == 'admin']
Do you mind providing an example of what you want to end up with? I want to make sure I'm understanding your needs. This was my understanding of what you're asking for:
data = {'owner': {'email': 'email@address'},'users': [{'username': 'FrankT','memberType': 'member'}, {'username': 'Chris','memberType': 'admin'}, {'username': 'Test','memberType': 'admin'}]}
email = data['owner']['email']
users = data['users']
admins = [{'email': email, **user} for user in users if user['memberType'] == 'admin']
Thank you very much @EarlMedina
One question... what is the **user in the below example from you?
admins = [{'email': email, **user} for user in users if user['memberType'] == 'admin']