[Tutor] If/elif/else when a list is empty

Steven D'Aprano steve at pearwood.info
Tue Sep 14 13:45:29 CEST 2010


On Tue, 14 Sep 2010 02:08:43 pm Vince Spicer wrote:

> Hey Tyler you can simplify this with a onliner.
>
> rg1, rg2, rg3 = rgenre + ["NA"]*(3-len(rgenre[:3]))

There's no real need to calculate the exact length that you want:

rg1, rg2, rg3 = (rgenre + ["NA"]*3)[:3]

For small numbers of items -- and three is pretty small -- that will be 
plenty efficient enough. If you had thousands of items, perhaps not, 
but for three, it's not worth the effort to try to be more clever.



-- 
Steven D'Aprano


More information about the Tutor mailing list