[Tutor] Newb ?

Liam Clarke-Hutchinson Liam.Clarke-Hutchinson at business.govt.nz
Thu Nov 17 21:43:57 CET 2005

>list() takes any iterable and returns a list object.
Hmm, I did not know that the list(str) thing worked that way.That'll reduce
the list comprehensions, but is it intuitive? Would a newbie see that and
think that list("Hi") returns ["Hi"] or ["H","i"] ?
>reversedMessage = ''.join(reversed(list(message)))

Yes, but for clarity when demonstrating a concept, one-liners should be
minimised, I think. ;)
But I agree, you can create some obscenely complicated one liners, which are
still understandable.
I find I have to force myself sometimes to refactor some of the ones I come
up with, however. Good rule of thumb is when you're feeling impressed with
your own cleverness, you probably need to refactor. 
(Thanks c2 wiki, for teaching me that.)

-----Original Message-----
From: Christian Wyglendowski [mailto:Christian.Wyglendowski at greenville.edu] 
Sent: Friday, 18 November 2005 9:31 a.m.
To: Liam Clarke-Hutchinson; tutor at python.org
Subject: RE: [Tutor] Newb ?

Liam said:
> How about -
> print "\n\nWelcome to the Backwards Message Display."
> print
> message = raw_input("\nPlease Enter a Message.")
> msgAsList = [ char for char in message]

You could also do:

msgAsList = list(message)

list() takes any iterable and returns a list object.

> msgAsList.reverse()
> reversedMessage = ''.join(msgAsList)

In Python 2.4, the following is also possible:

reversedMessage = ''.join(reversed(list(message)))

It's amazing how in Python even one-liners can be so pretty :-)


A new monthly electronic newsletter covering all aspects of MED's work is now available.  Subscribers can choose to receive news from any or all of seven categories, free of charge: Growth and Innovation, Strategic Directions, Energy and Resources, Business News, ICT, Consumer Issues and Tourism.  See http://news.business.govt.nz for more details.

http://www.govt.nz - connecting you to New Zealand central & local government services

Any opinions expressed in this message are not necessarily those of the Ministry of Economic Development. This message and any files transmitted with it are confidential and solely for the use of the intended recipient. If you are not the intended recipient or the person responsible for delivery to the intended recipient, be advised that you have received this message in error and that any use is strictly prohibited. Please contact the sender and delete the message and any attachment from your computer.

More information about the Tutor mailing list