What's an elegant way to test for list index existing?
Alister
alister.ware at ntlworld.com
Sat Sep 29 05:27:10 EDT 2018
On Fri, 28 Sep 2018 19:00:29 +0100, Chris Green 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.
how about simply
adding 2 or more null entries to the list then slicing?
string = "a b c"
a=string.split()
(a+['',''])[0:5]
--
QOTD:
If you're looking for trouble, I can offer you a wide selection.
More information about the Python-list
mailing list