Python >= 3.5 alternative: unpack into a list literal [*newdict]
New unpacking generalizationunpacking generalizations (PEP 448) were introduced with Python 3.5, now allowing you canto now easily do:
>>> newdict = {1:0, 2:0, 3:0} >>> [*newdict] [1, 2, 3] Unpacking with * works with any object that is iterable and, since dictionaries yieldreturn their keys when iterated through, you can easily create a list by using it within a list literal.
Adding .keys() i.e [*newdict.keys()] might help in making your intent a bit more explicit though it will cost you a function look-up and invocation. (which, in all honesty, isn't something you should really be worried about).
It isThough equivalent to list(newdict) with the difference that it's faster (at least for small dictionaries) because no function call is actually performed:
%timeit [*newdict] 1000000 loops, best of 3: 249 ns per loop %timeit list(newdict) 1000000 loops, best of 3: 508 ns per loop %timeit [k for k in newdict] 1000000 loops, best of 3: 574 ns per loop with larger dictionaries the speed is pretty much the same (the overhead of iterating through a large collection trumps the small cost of a function call).
In a similar fashion, you can create tuples and sets of dictionary keys:
>>> *newdict, (1, 2, 3) >>> {*newdict} {1, 2, 3} beware of the trailing comma in the tuple case!