Is there a better way to create a list of None objects?

Chris Angelico rosuav at gmail.com
Thu Aug 12 05:11:06 EDT 2021


On Thu, Aug 12, 2021 at 6:59 PM Stephen Tucker <stephen_tucker at sil.org> wrote:
>
> Hi,
>
> I thought I'd share the following piece of code that I have recently written
> (a) to check that what I have done is reasonable - even optimum,
> (b) to inform others who might be wanting to do similar things, and
> (c) to invite comment from the community.
>
> -------------------------------------------
>
> #
>
> # Yes: Create an empty list of Band Limits for this language
>
> #
>
> # Note. The rather complicated logic on the right-hand side of the
>
> #       assignment below is used here because none of the following
>
> #       alternatives had the desired effect:
>
> #
>
> # Logic             Effect
>
> #
>
> # [None * 8]        TypeError: unsupported operand type(s) for *: ...
>
> # [(None) * 8]      TypeError: unsupported operand type(s) for *: ...
>
> # [((None)) * 8]    TypeError: unsupported operand type(s) for *: ...
>
> # [(None,) * 8]     [(None, None, None, None, None, None, None, None)]
>
> # list ((None) * 8) TypeError: unsupported operand type(s) for *: ...
>
> #
>
> diclll_BLim [thisISO_] = list ((None,) * 8)
>

Why not just:

[None] * 8

?

ChrisA


More information about the Python-list mailing list