Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 620 characters in body
Source Link

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).

The `*iterable` syntax is similar to doing `list(iterable)` and its behaviour was initially documented in the [Calls section][2] of the Python Reference manual. With PEP 448 the restriction on where `*iterable` could appear was loosened allowing it to also be placed in list, set and tuple literals, the reference manual on [Expression lists][3] was also updated to state this.

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!

Python >= 3.5 alternative: unpack into a list literal [*newdict]

New unpacking generalization were introduced with Python 3.5, now you can 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 yield 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 is 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!

Python >= 3.5 alternative: unpack into a list literal [*newdict]

New unpacking generalizations (PEP 448) were introduced with Python 3.5 allowing you to 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 return 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).

The `*iterable` syntax is similar to doing `list(iterable)` and its behaviour was initially documented in the [Calls section][2] of the Python Reference manual. With PEP 448 the restriction on where `*iterable` could appear was loosened allowing it to also be placed in list, set and tuple literals, the reference manual on [Expression lists][3] was also updated to state this.

Though 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!

added 136 characters in body
Source Link

Python >= 3.5 alternative: unpack into a list literal [*newdict]

New unpacking generalization wherewere introduced with Python 3.5, now you can easily do:

>>> newdict = {1:0, 2:0, 3:0} >>> [*newdict] [1, 2, 3] 

Unpacking with * works with any iterable object that is iterable and, since dictionaries yield 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 is 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!

Python >= 3.5 alternative: unpack into a list literal [*newdict]

New unpacking generalization where introduced with Python 3.5, now you can easily do:

>>> newdict = {1:0, 2:0, 3:0} >>> [*newdict] [1, 2, 3] 

Unpacking with * works with any iterable object and, since dictionaries yield their keys when iterated through, you can easily create a list by using it. 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 is 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.


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!

Python >= 3.5 alternative: unpack into a list literal [*newdict]

New unpacking generalization were introduced with Python 3.5, now you can 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 yield 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 is 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!

added 231 characters in body
Source Link

Python >= 3.5 alternative: unpack into a list literal [*newdict]

New unpacking generalization where introduced with Python 3.5, now you can easily do:

>>> newdict = {1:0, 2:0, 3:0} >>> [*newdict] [1, 2, 3] 

Unpacking with * works with any iterable object and, since dictionaries yield their keys when iterated through, you can easily create a list by using it. 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 is equivalent to list(newdict) with the difference that it's fasterfaster (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.


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!

Python >= 3.5 alternative: unpack into a list literal [*newdict]

New unpacking generalization where introduced with Python 3.5, now you can easily do:

>>> newdict = {1:0, 2:0, 3:0} >>> [*newdict] [1, 2, 3] 

Unpacking with * works with any iterable object and, since dictionaries yield their keys when iterated through, you can easily create a list by using it.

It is 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.


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!

Python >= 3.5 alternative: unpack into a list literal [*newdict]

New unpacking generalization where introduced with Python 3.5, now you can easily do:

>>> newdict = {1:0, 2:0, 3:0} >>> [*newdict] [1, 2, 3] 

Unpacking with * works with any iterable object and, since dictionaries yield their keys when iterated through, you can easily create a list by using it. 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 is 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.


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!

added 61 characters in body
Source Link
Loading
added 205 characters in body
Source Link
Loading
added 297 characters in body
Source Link
Loading
added 319 characters in body
Source Link
Loading
Source Link
Loading