Select to view content in your preferred language

read and write to variable from DICT

646
3
Jump to solution
12-07-2022 11:49 AM
kapalczynski
Frequent Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
EarlMedina
Esri Regular Contributor

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']

View solution in original post

3 Replies
EarlMedina
Esri Regular Contributor

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']
kapalczynski
Frequent Contributor

Thank you very much @EarlMedina 

0 Kudos
kapalczynski
Frequent Contributor

One question... what is the **user in the below example from you?

admins = [{'email': email, **user} for user in users if user['memberType'] == 'admin']

 

0 Kudos