Can anybody explain the '-' in a 2-D creation code?

Gary Herron gary.herron at islandtraining.com
Thu Jun 25 21:18:15 EDT 2015


On 06/25/2015 06:07 PM, fl wrote:
> Hi,
>
> I read Ned's tutorial on Python. It is very interesting. On its last
> example, I cannot understand the '_' in:
>
>
>
> board=[[0]*8 for _ in range(8)]
>
>
> I know  '_' is the precious answer, but it is still unclear what it is
> in the above line. Can you explain it to me?
>
>
> Thanks,

He uses _ to indicate a variable whose name and value he does not care 
about, but it *is* a valid variable name.

He could have used
   ... for i in range ...
or
   ... for unused_variable in range ...

This is a valid, though probably unclear, use of that same name:

 >>> _ = 123
 >>> print(_)
123
 >>>


Gary Herron






More information about the Python-list mailing list