[Tutor] split struggle

Steven D'Aprano steve at pearwood.info
Wed Jun 23 00:11:04 CEST 2010

On Wed, 23 Jun 2010 06:58:25 am Richard D. Moores wrote:
> Please see my Python 3.1 code pasted at
> <http://python.pastebin.com/T3vm9vKT>.
> This does what I want, which is to do one of:
> 1. print all the elements of the list, lst.
> 2. print "Done" when "" is entered.
> 3. print the elements of lst whose indexes are entered.
> (sorry if all this is obvious)
> Now, the code works, but isn't there a better way to do what I want?
> I've been away from Python for a while, and have gotten rusty.

Seems reasonable to me. Sometimes the simple things are the best.

The only thing I'd do is pre-process the "all" case:

lst = [100,101,102,103]
indexes = input("'Enter indexes': ").split(",")
if indexes == ["all"]:
    indexes = list(range(len(lst))
if indexes == [""]:
    indexes = [int(k) for k in indexes]
    for i in indexes:

Steven D'Aprano

More information about the Tutor mailing list