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

2018-09-30 Thread Alister via Python-list
On Sun, 30 Sep 2018 11:45:21 +0100, Bart wrote: > On 30/09/2018 11:14, Chris Green wrote: >> Chris Angelico wrote: >>> On Sat, Sep 29, 2018 at 12:21 PM Chris Green wrote: I have a list created by:- fld = shlex.split(ln) It may contain 3, 4 or 5 entries

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

2018-09-30 Thread Chris Green
Bart wrote: > On 30/09/2018 11:14, Chris Green wrote: > > Chris Angelico wrote: > >> On Sat, Sep 29, 2018 at 12:21 PM 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. >

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

2018-09-30 Thread Chris Green
Chris Angelico wrote: > On Sat, Sep 29, 2018 at 12:21 PM 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 > >

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

2018-09-30 Thread Chris Green
Glen D souza wrote: > i have a approach, it may not be best > > fld = [ ] > for data in shlex.split(ln): >fld.append(data) > It's certainly simple! :-) I (OP) have actually done something quite similar:- fld = shlex.split(ln) fld.append(999) fld.append(999) It means I

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

2018-09-30 Thread Peter Otten
Glen D souza wrote: > fld = [ ] > data = shlex.split(ln) > for item in data: >fld.append(item) > fld = fld + [0] * (5 - len(data)) There's no need to make a copy of data, one item at the time. It's a tedious way to build a new list, and you are throwing it away in the next line anyway,

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

2018-09-29 Thread Glen D souza
i have a approach, it may not be best fld = [ ] for data in shlex.split(ln): fld.append(data) On Sat, 29 Sep 2018 at 07:52, wrote: > On Friday, September 28, 2018 at 11:03:17 AM UTC-7, Chris Green wrote: > > I have a list created by:- > > > > fld = shlex.split(ln) > > > > It may

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

2018-09-29 Thread Glen D souza
fld = [ ] data = shlex.split(ln) for item in data: fld.append(item) fld = fld + [0] * (5 - len(data)) On Sat, 29 Sep 2018 at 11:03, Glen D souza wrote: > i have a approach, it may not be best > > fld = [ ] > for data in shlex.split(ln): >fld.append(data) > > > > On Sat, 29 Sep

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

2018-09-29 Thread Chris Angelico
On Sat, Sep 29, 2018 at 12:21 PM 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?

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

2018-09-29 Thread Alister via Python-list
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?

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

2018-09-29 Thread Dan Sommers
On 9/28/18 2:00 PM, 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:'

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

2018-09-29 Thread Peter Otten
Ben Finney wrote: > Ben Finney writes: > >> You can use a comprehension, iterating over the full range of index you >> want:: >> >> words = shlex.split(line) >> padding_length = 5 >> words_padded = [ >> (words[index] if index < len(words)) >> for index in

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

2018-09-29 Thread Ben Finney
Ben Finney writes: > You can use a comprehension, iterating over the full range of index you > want:: > > words = shlex.split(line) > padding_length = 5 > words_padded = [ > (words[index] if index < len(words)) > for index in range(padding_length)] That omits the

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

2018-09-29 Thread Chris Green
Thanks all, several possible ways of doing it there. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list

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

2018-09-29 Thread Peter Otten
jlada...@itu.edu wrote: > On Friday, September 28, 2018 at 11:03:17 AM UTC-7, 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

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

2018-09-28 Thread Ben Finney
Chris Green writes: > I have a list created by:- > > fld = shlex.split(ln) > > It may contain 3, 4 or 5 entries according to data read into ln. Because of what an index means for the 'list' type, that's equivalent to saying "the result of `len(fld)` may be 3, 4, or 5". > What's the neatest

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

2018-09-28 Thread jladasky
On Friday, September 28, 2018 at 11:03:17 AM UTC-7, 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

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

2018-09-28 Thread Chris Green
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. -- Chris Green · --