2 ms·
It's not exactly the same, which is what I thought was interesting. My first example did use a generator expression inside the dict() constructor, but in that c
by delluminatus 11y ago
It's not exactly the same, which is what I thought was interesting. My first example did use a generator expression inside the dict() constructor, but in that case you need to specify the key and value in a tuple or a list.
With the dictionary comprehension you can just separate the key and value with a colon, which is more natural. It might just be sugar on top of a generator expression but it is definitely a special case, syntactically speaking.
- aaronchall 11y agoYes, in Python 2.7 onward: s = {i**2 for i in range(10) if i} and the colon is what makes the dict comprehension different from the set comprehension: d - {i:i**2 for i in range(10) if i}