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?