Python Forum

Full Version: dictionary comprehension
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how can i make a dictionary comprehension for the dictionary being built in this code snippet?

aws_regions = [
    ('us-east-1',      'use1', 'N. Virginia'   ),
    ('us-east-2',      'use2', 'Ohio'          ),
    ('us-west-1',      'usw1', 'N. California' ),
    ('us-west-2',      'usw2', 'Oregon'        ),
    ('ca-central-1',   'cac1', 'Central'       ),
    ('eu-west-1',      'euw1', 'Ireland'       ),
    ('eu-west-2',      'euw2', 'London'        ),
    ('eu-central-1',   'euc1', 'Frankfurt'     ),
    ('ap-southeast-1', 'ase1', 'Singapore'     ),
    ('ap-northeast-1', 'ane1', 'Tokyo'         ),
    ('ap-northeast-2', 'ane2', 'Seoul'         ),
    ('ap-southeast-2', 'ase2', 'Sydney'        ),
    ('ap-south-1',     'aps1', 'Mumbai'        ),
    ('sa-east-1',      'sae1', 'Sao Paulo'     ),
]

aws_regions_dict = {}
for r in aws_regions:
    aws_regions_dict[r[0]] = r
    aws_regions_dict[r[1]] = r
this dictionary will have keys from both long names in the first column and short names in the second column.  this is what i don't how to do in comprehsions.
aws_regions = [
    ('us-east-1',      'use1', 'N. Virginia'   ),
    ('us-east-2',      'use2', 'Ohio'          ),
    ('us-west-1',      'usw1', 'N. California' ),
    ('us-west-2',      'usw2', 'Oregon'        ),
    ('ca-central-1',   'cac1', 'Central'       ),
    ('eu-west-1',      'euw1', 'Ireland'       ),
    ('eu-west-2',      'euw2', 'London'        ),
    ('eu-central-1',   'euc1', 'Frankfurt'     ),
    ('ap-southeast-1', 'ase1', 'Singapore'     ),
    ('ap-northeast-1', 'ane1', 'Tokyo'         ),
    ('ap-northeast-2', 'ane2', 'Seoul'         ),
    ('ap-southeast-2', 'ase2', 'Sydney'        ),
    ('ap-south-1',     'aps1', 'Mumbai'        ),
    ('sa-east-1',      'sae1', 'Sao Paulo'     ),
]

aws_regions_dict = {k:r for r in aws_regions for k in r[:2]}