2

In python, I can use the splat operator to unpack a list and send the list items as arguments to a function

dirs = ['this','is','a','file','path']
path = os.path.join(*dirs)
# path is now 'this/is/a/file/path'

My question is, why is this feature limited to method params?

For example, the following is invalid syntax:

x = [1,2,3]
y = [0,*x,4,5]

Why doesn't this result in [0,1,2,3,4,5]?

JordanC
  • 1,303
  • 12
  • 28

1 Answers1

4

As mentioned, it is valid syntax as of Python 3.5+:

>>> x = [1,2,3]
>>> y = [0,*x,4,5]
>>> y
[0, 1, 2, 3, 4, 5]

You can read about this and more use cases in PEP 448: "Additional Unpacking Generalizations".

brianpck
  • 8,084
  • 1
  • 22
  • 33