In Python 2.7.15:
>>> dict((s, len(s)) for s in ['aa', 'b', 'cccc'])
{'aa': 2, 'cccc': 4, 'b': 1}
>>> {(s, len(s)) for s in ['aa', 'b', 'cccc']}
set([('b', 1), ('aa', 2), ('cccc', 4)])
Why does dict()
create a dictionary, but {}
creates a set? I thought those two syntaxes were synonymous.