Iterate over a dictionary by comprehension and get a dictionary

23,974

Solution 1

The expression:

{ value for bar in iterable }

is a set comprehension.

In order to do a dict comprehension, you have to provide Python with a set of key-value pairs separated by ::

{ key: value for bar in iterable }

Solution 2

Replace

{(key,val) for key, val in mime_types.items() if "image/tiff" == val}

with

{key: val for key, val in mime_types.items() if "image/tiff" == val}

Solution 3

You can do dictionary comprehension as @Anubhav Chattoraj suggested.

Or pass a generator expr as an argument to function dict:

In [165]: dict((k, mimes[k]) for k in mimes if mimes[k] == "image/tiff")
Out[165]: {'.tif': 'image/tiff', '.tiff': 'image/tiff'}

Don't mix the two ways up..

Solution 4

you can try something like this

>>> print {k : v for k, v in mime_types.iteritems()}

Another Simple Example

    >>> print {i : chr(65+i) for i in range(4)}
    {0 : 'A', 1 : 'B', 2 : 'C', 3 : 'D'}
Share:
23,974

Related videos on Youtube

Laxmikant Ratnaparkhi
Author by

Laxmikant Ratnaparkhi

Updated on March 07, 2020

Comments

  • Laxmikant Ratnaparkhi
    Laxmikant Ratnaparkhi about 4 years

    How to iterate over a dictionary by dictionary comprehension to process it.

    >>> mime_types={
        '.xbm': 'image/x-xbitmap',
        '.dwg': 'image/vnd.dwg',
        '.fst': 'image/vnd.fst',
        '.tif': 'image/tiff',
        '.gif': 'image/gif',
        '.ras': 'image/x-cmu-raster',
        '.pic': 'image/x-pict',
        '.fh':  'image/x-freehand',
        '.djvu':'image/vnd.djvu',
        '.ppm': 'image/x-portable-pixmap',
        '.fh4': 'image/x-freehand',
        '.cgm': 'image/cgm',
        '.xwd': 'image/x-xwindowdump',
        '.g3':  'image/g3fax',
        '.png': 'image/png',
        '.npx': 'image/vnd.net-fpx',
        '.rlc': 'image/vnd.fujixerox.edmics-rlc',
        '.svgz':'image/svg+xml',
        '.mmr': 'image/vnd.fujixerox.edmics-mmr',
        '.psd': 'image/vnd.adobe.photoshop',
        '.oti': 'application/vnd.oasis.opendocument.image-template',
        '.tiff':'image/tiff',
        '.wbmp':'image/vnd.wap.wbmp'
    }
    
    >>> {(key,val) for key, val in mime_types.items() if "image/tiff" == val}
    

    This is returning result like this:

    set([('.tiff', 'image/tiff'), ('.tif', 'image/tiff')])
    

    But I'm expecting

    ('.tif', 'image/tiff')
    

    How can I modify that result to get a dictionary like :

    {'.tif': 'image/tiff'}
    
  • thefourtheye
    thefourtheye about 10 years
    It would have been better if you have avoided the .items()
  • zhangxaochen
    zhangxaochen about 10 years
    @thefourtheye why? won't you get ValueError: too many values to unpack?
  • thefourtheye
    thefourtheye about 10 years
    dict((k, mimes[k]) for k in mimes if mimes[k] == image/tiff") You ll be avoiding the intermediatate list generated by .items()
  • zhangxaochen
    zhangxaochen about 10 years
    @thefourtheye I'm using python3 just now... anyway answer updated ;)
  • thefourtheye
    thefourtheye about 10 years
    Actually this will work consistently in both Python2 and Python3. But, items() will return an iterator in Py3 but a list in Py2.
  • Charlie Parker
    Charlie Parker over 4 years
    just in case anyone else messes up, the .items() call on the dictionary is crucial.