Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over a dictionary by comprehension and get a dictionary [duplicate]

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'}
like image 432
Laxmikant Ratnaparkhi Avatar asked Mar 07 '14 07:03

Laxmikant Ratnaparkhi


People also ask

Can you iterate over a dictionary?

You can loop through a dictionary by using a for loop. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

Can you do dictionary comprehension in Python?

Like List Comprehension, Python allows dictionary comprehensions. We can create dictionaries using simple expressions.

Is dictionary comprehension faster than for loop?

List comprehensions are faster than for loops to create lists. But, this is because we are creating a list by appending new elements to it at each iteration.

How do you iterate through a dictionary backwards?

Another simple solution to iterate over a dictionary in the sorted order of keys is to use the dict. keys() with the sorted() function. Alternatively, to iterate in reverse order of keys, you can specify the reverse argument of the sorted() function as True .


4 Answers

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}
like image 88
Anubhav C Avatar answered Oct 09 '22 06:10

Anubhav C


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 }
like image 21
Joel Cornett Avatar answered Oct 09 '22 07:10

Joel Cornett


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..

like image 6
zhangxaochen Avatar answered Oct 09 '22 08:10

zhangxaochen


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'}
like image 1
Pavan Gupta Avatar answered Oct 09 '22 06:10

Pavan Gupta