2

In Python 3, you can print a bunch of objects* with a variable number of objects as the first bunch of args:

For example:

print(192,168,178,42,sep=".")

Or for example:

print("09","03","2018",sep="-")

But like let's say I have a collection of [192,168,178,42] and I want to pass that to printed with a separator...how do I "unbox it into formal arguments" (and I'm using the term loosely) into arguments?

leeand00
  • 25,510
  • 39
  • 140
  • 297

1 Answers1

3

Use the * unpacking operator:

print(*[192,168,178,42],sep=".")

or with a variable:

mylist = [192,168,178,42]
print(*mylist, sep=".")

Output:

192.168.178.42

See here for more details on packing/unpacking in Python.

iz_
  • 15,923
  • 3
  • 25
  • 40
  • FTW there is also a unpack dict opertator `**` – leeand00 Jan 08 '19 at 03:45
  • 1
    @leeand00 Yes, but it only works for keyword args, not normal positional args. If you try to unpack a `dict` with `*`, it will unpack the keys. – iz_ Jan 08 '19 at 03:46