Skip to main content

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5.0b1Python 3.5, manipulation of lists, tuples, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T] tuple(T) (*T,) 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpacking is shorter:

L=[*T] *L,=T 

A similar syntax works for tuples:

T=*L, 

which is like extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list/tuple to both sides:

[1]+T+[2] [1,*T,2] (1,)+T+(2,) (1,*T,2) 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

This basically negates any need for dict.update.

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5.0b1, manipulation of lists, tuples, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T] tuple(T) (*T,) 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpacking is shorter:

L=[*T] *L,=T 

A similar syntax works for tuples:

T=*L, 

which is like extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list/tuple to both sides:

[1]+T+[2] [1,*T,2] (1,)+T+(2,) (1,*T,2) 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

This basically negates any need for dict.update.

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5, manipulation of lists, tuples, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T] tuple(T) (*T,) 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpacking is shorter:

L=[*T] *L,=T 

A similar syntax works for tuples:

T=*L, 

which is like extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list/tuple to both sides:

[1]+T+[2] [1,*T,2] (1,)+T+(2,) (1,*T,2) 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

This basically negates any need for dict.update.

deleted 52 characters in body
Source Link
Sp3000
  • 62.3k
  • 13
  • 117
  • 292

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5.0b1, manipulation of lists, tuples, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T]   tuple(T) (*T,) 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpacking is shorter:

L=[*T] *L,=T 

Turning an iterable into a tuple

This one's less useful since it's not an expression, but you can turn something into a tuple and assign to a variable viaA similar syntax works for tuples:

T=*L, 

which looks similar tois like extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list/tuple to both sides:

[1]+T+[2] [1,*T,2] 

You can get similar savings with tuples, but as aforementioned it's unfortunately not an expression:

T=  (1,)+T+T+(2,) T=1(1,*T,2) 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

This basically negates any need for dict.update.

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5.0b1, manipulation of lists, tuples, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T] 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpacking is shorter:

L=[*T] *L,=T 

Turning an iterable into a tuple

This one's less useful since it's not an expression, but you can turn something into a tuple and assign to a variable via

T=*L, 

which looks similar to extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list to both sides:

[1]+T+[2] [1,*T,2] 

You can get similar savings with tuples, but as aforementioned it's unfortunately not an expression:

T=(1,)+T T=1,*T 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

This basically negates any need for dict.update.

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5.0b1, manipulation of lists, tuples, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T]   tuple(T) (*T,) 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpacking is shorter:

L=[*T] *L,=T 

A similar syntax works for tuples:

T=*L, 

which is like extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list/tuple to both sides:

[1]+T+[2] [1,*T,2]   (1,)+T+(2,) (1,*T,2) 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

This basically negates any need for dict.update.

added 98 characters in body
Source Link
Sp3000
  • 62.3k
  • 13
  • 117
  • 292

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5.0b1, manipulation of lists, tuples, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T] 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpackingextended iterable unpacking is shorter:

L=[*T] *L,=T 

Turning an iterable into a tuple

This one's less useful since it's not an expression, but you can turn something into a tuple and assign to a variable via

T=*L, 

which looks similar to extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list to both sides:

[1]+T+[2] [1,*T,2] 

The saving is much better forYou can get similar savings with tuples, but as aforementioned it's unfortunately not an expression:

T=(1,)+T T=1,*T 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

This basically negates any need for dict.update.

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5.0b1, manipulation of lists, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T] 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpacking is shorter:

L=[*T] *L,=T 

Turning an iterable into a tuple

This one's less useful since it's not an expression, but you can turn something into a tuple and assign to a variable via

T=*L, 

which looks similar to extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list to both sides:

[1]+T+[2] [1,*T,2] 

The saving is much better for tuples, but as aforementioned it's unfortunately not an expression:

T=(1,)+T T=1,*T 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

PEP448 – Additional Unpacking Generalizations

With the release of Python 3.5.0b1, manipulation of lists, tuples, sets and dicts just got golfier.

Turning an iterable into a set/list

Compare the pairs:

set(T) {*T} list(T) [*T] 

Much shorter! Note, however, that if you just want to convert something to a list and assign it to a variable, normal extended iterable unpacking is shorter:

L=[*T] *L,=T 

Turning an iterable into a tuple

This one's less useful since it's not an expression, but you can turn something into a tuple and assign to a variable via

T=*L, 

which looks similar to extended iterable unpacking, but with the asterisk and comma on the other side.

Joining lists/tuples

Unpacking is slightly shorter than concatenation if you need to append a list to both sides:

[1]+T+[2] [1,*T,2] 

You can get similar savings with tuples, but as aforementioned it's unfortunately not an expression:

T=(1,)+T T=1,*T 

Printing the contents of multiple lists

This isn't limited to print, but it's definitely where most of the mileage will come from. PEP448 now allows for multiple unpacking, like so:

>>> T = (1, 2, 3) >>> L = [4, 5, 6] >>> print(*T,*L) 1 2 3 4 5 6 

Updating multiple dictionary items

This probably won't happen very often, but the syntax can be used to save on updating dictionaries if you're updating at least three items:

d[0]=1;d[1]=3;d[2]=5 d={**d,0:1,1:3,2:5} 

This basically negates any need for dict.update.

Source Link
Sp3000
  • 62.3k
  • 13
  • 117
  • 292
Loading