Hello Steven,<br><br>Thanks a lot for the detailed answer. I will implement your suggestions. <br>Really appreciate it.<br><br>Thanks and Regards,<br>Sumod<br><br><div class="gmail_quote">On Fri, Jan 27, 2012 at 4:34 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><a href="mailto:spawgi@gmail.com" target="_blank">spawgi@gmail.com</a> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello,<br>
<br>
My code is -<br>
<br>
l = len(m)<br>
item = str(m[1])<br>
for i in range(2,l):<br>
item = item + "-" + str(m[i])<br>
<br>
This code is part of a bigger function. It works fine. But I am not happy<br>
with the way I have written it. I think there is a better (Pythonic) way to<br>
rewrite it.<br>
If anyone knows how to improve this snippet, I would be very thankful.<br>
</blockquote>
<br></div>
(1) Never use "l" as a variable name, as it looks too much like 1.<br>
<br>
<br>
(2) Use meaningful names. When posting snippets for help, you should show example data.<br>
<br>
<br>
(3) You almost never need to iterate over an index by hand. Instead iterate over the items themselves. That is, instead of this:<br>
<br>
for i in len(sequence):<br>
do_something_with( sequence[i] )<br>
<br>
do this instead:<br>
<br>
for item in sequence:<br>
do_something_with( item )<br>
<br>
<br>
and similar variations.<br>
<br>
<br>
(4) When you want only part of a sequence, use slicing to extract just the parts you care about.<br>
<br>
<br>
(5) When assembling strings from substrings, never use repeated concatenation using + as that can be EXTREMELY slow. Use str.join to build the string in one assignment, instead of multiple assignments.<br>
<br>
Your code shown above is *very* inefficient and will be PAINFULLY slow if m is very large. To understand why, you should read this article:<br>
<br>
<a href="http://www.joelonsoftware.com/articles/fog0000000319.html" target="_blank">http://www.joelonsoftware.com/<u></u>articles/fog0000000319.html</a><br>
<br>
In this case, you can replace your snippet with this:<br>
<br>
result = '-'.join(str(item) for item in m[1:])<br>
<br>
<br>
<br>
For example:<br>
<br>
py> m = [1, 2, "hello world", (23, 42), 5]<br>
py> '-'.join(str(item) for item in m[1:])<br>
'2-hello world-(23, 42)-5'<br><font color="#888888">
<br>
<br>
<br>
-- <br>
Steven<br>
<br>
______________________________<u></u>_________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/tutor</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br><a href="http://spawgi.wordpress.com" target="_blank">http://spawgi.wordpress.com</a><br>We can do it and do it better.<br>