What's an elegant way to test for list index existing?

Chris Angelico rosuav at gmail.com
Fri Sep 28 22:35:31 EDT 2018


On Sat, Sep 29, 2018 at 12:21 PM Chris Green <cl at isbd.net> wrote:
>
> I have a list created by:-
>
>     fld = shlex.split(ln)
>
> It may contain 3, 4 or 5 entries according to data read into ln.
> What's the neatest way of setting the fourth and fifth entries to an
> empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels
> clumsy somehow.

shlex.split(ln) + ["", ""]

ChrisA



More information about the Python-list mailing list