On 03/03/2017 12:19 PM, Alan Gauld via Tutor wrote:
On 03/03/17 18:52, Peter Otten wrote:
Antonio Zagheni via Tutor wrote:

suitcase = ["book, ", "towel, ", "shirt, ", "pants"]
Hm, looks like you opened Rafael's suitcase while he wasn't looking, and
sneaked in some commas and spaces ;)

That's cheating...
Its also very difficult to maintain since if you add
new items to the suitcase you need to make sure they
all have commas except the last one. And inconsistent data formatting in
a list is a nightmare.

For example, what happens if you decide to sort the list,
the last item is no longer last and the commas are all
messed up.

That's one reason why join() is a better solution, it
handles all of that for you. It's also faster, although
in a small application you'd never notice the difference.

The ','.join(suitcase) is obviously best of all, but if one doesn't know that method, the below suggestion can be fixed with:

suitcase = ['book', 'towel', 'shirt', 'pants']

for i in suitcase:
    st = st + i + ', '

print('You have a s% in your luggage.' % st)


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to