python
[Python] Using comprehension expression to convert list of dictionaries to nested dictionary
# yaml file used for account
- sitename: testcomp
email: [email protected]
pass: 111
role: admin
name: admin
- sitename: testcomp
email: [email protected]
pass: 111
role: edit
name: user1
- sitename: testcomp
email: [email protected]
pass: 11
role: edit
name: user2
# convert.py
# read yaml file into faccounts
faccounts = yaml.load(file, Loader=yaml.FullLoader)
# Convert list of dictionaries to nested dictionary so that we can select any value by user name and key nam
account = {account['name']: account for account in faccounts}
return account
Was this helpful?
Similar Posts
- Python code examples to flatten a nested dictionary
- Convert pandas DataFrame to List of dictionaries python
- Convert JSON string to Python collections - like list, dictionaries
- Get all values by key name from a list of dictionaries Python
- Python collection - Dictionaries in python
- Merge two or multiple dictionaries in Python
- Check if two dictionaries are equal in Python