Top "Dictionary-comprehension" questions

A syntactic construct in Python which provides a concise way to create dictionaries.

Create a dictionary with list comprehension

I like the Python list comprehension syntax. Can it be used to create dictionaries too? For example, by iterating over …

python dictionary list-comprehension dictionary-comprehension
Why is there no tuple comprehension in Python?

As we all know, there's list comprehension, like [i for i in [1, 2, 3, 4]] and there is dictionary comprehension, like {i:j …

python tuples list-comprehension dictionary-comprehension set-comprehension
How can I use if/else in a dictionary comprehension?

Does there exist a way in Python 2.7+ to make something like the following? { something_if_true if condition else something_…

python dictionary dictionary-comprehension
Build Dictionary in Python Loop - List and Dictionary Comprehensions

I'm playing with some loops in python. I am quite familiar with using the "for" loop: for x in y: …

python for-loop list-comprehension dictionary-comprehension
Nested dictionary comprehension python

I'm having trouble understanding nested dictionary comprehensions in Python 3. The result I'm getting from the example below outputs the correct …

python syntax nested list-comprehension dictionary-comprehension
Return copy of dictionary excluding specified keys

I want to make a function that returns a copy of a dictionary excluding keys specified in a list. Considering …

python dictionary dictionary-comprehension
Conditional expressions in Python Dictionary comprehensions

a = {"hello" : "world", "cat":"bat"} # Trying to achieve this # Form a new dictionary only with keys with "hello" and their …

python dictionary-comprehension
creating dict where keys are alphabet letters and values are 1-26 using dict comprehension

alphaValueDict = OrderedDict.fromkeys(string.ascii_uppercase,range(0) i = 1 for k,v in alphaValueDict.iteritems(): alphaValueDict[k] = [i] i += 1 return alphaValueDict …

python dictionary dictionary-comprehension
multiple key value pairs in dict comprehension

I am trying to create multiple key : value pairs in a dict comprehension like this: {'ID': (e[0]), 'post_author': (e[1]) …

python dictionary dictionary-comprehension
Iterate over a dictionary by comprehension and get a dictionary

How to iterate over a dictionary by dictionary comprehension to process it. >>> mime_types={ '.xbm': 'image/…

python dictionary dictionary-comprehension