|
|
|
|
|
by eckzow
4878 days ago
|
|
That's funny--as a python guy I was squirming for a set comprehension immediately: >>> a = {'a':1,'b':2}
>>> b = {'b':3,'c':4}
>>> def keylist(*maps):
... return ', '.join(sorted({str(k) for m in maps for k in m.keys()})) or '<none>'
...
>>> keylist(a, b)
'a, b, c'
>>> keylist()
'<none>'
I'm sure people will have differing opinions on the readability of a nested set comprehension, but from the bullets at the top of the article to one-liner implementation in ~30 seconds. (For an arbitrary number of maps, no less.)(edit: added '<none>' on empty, fixed formatting, whoops now I'm over 30 seconds :)) |
|