[Python] Variadic arguments can be used when unpacking iterable elements

Recently, I've been solving programming problems on a certain site to improve my python skills. Note that I noticed that variadic arguments can be used in the preprocessing of input data.

The input data of each question is given the following format from the standard input, for example.

Municipality Name Population Area
shinjyuku 341009 18.22
・ ・ ・

Multiple types on a line, especially a string starting with a string and all following a numeric pattern.

Since the data read from the standard input is stored as a character string type, Conversion is required for numerical data.

>>> name, population, area = input().split(' ')
shinjyuku 341009 18.22
>>> name
'shinjyuku'
>>> population
'341009'
>>> area
'18.22'

This is troublesome because it is necessary to perform numerical conversion for each variable. So, I want to put all the numerical parts together in the list.

>>> name, data = input().split(' ')
shinjyuku 341009 18.22
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)

If you simply try to assign multiple unpacked values to one variable data, I get angry when there are not enough variables. So we use variadic arguments.

>>> name, *data = input().split(' ')
shinjyuku 341009 18.22
>>> name
'shinjyuku'
>>> data
['341009', '18.22']

It is a variable length argument that you often see and hear when talking about method arguments, but there is also such a usage. However, in this case, it feels strange to call it "variadic argument".

Recommended Posts

[Python] Variadic arguments can be used when unpacking iterable elements
Scripts that can be used when using bottle in Python
[Python] Display list elements with variadic arguments
Python note: When the pip command cannot be used
Japanese can be used with Python in Docker environment
Python knowledge notes that can be used with AtCoder
Can be used in competition pros! Python standard library
[Python3] Code that can be used when you want to resize images in folder units
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
Can be used when aws-cli is available but jq is not available jp.py
New features in Python 3.9 (1)-Union operators can be used in dictionary types
Python standard input summary that can be used in competition pro
Python standard module that can be used on the command line
[Python] Be careful when using print
Python --Symbols used when performing operations
I created a template for a Python project that can be used universally
Mathematical optimization that can be used for free work with Python + PuLP
[Python3] Code that can be used when you want to cut out an image in a specific size
[Python3] Code that can be used when you want to change the extension of an image at once
EP 17 Be Defensive When Iterating Over Arguments
Summary of scikit-learn data sources that can be used when writing analysis articles
I made a familiar function that can be used in statistics with Python
How to install a Python library that can be used by pharmaceutical companies
Correspondence memo when the direction key cannot be used on the python command line