Mergesort problem

Andrea D'Amore and.damore at gmail.com
Thu Dec 22 08:58:35 EST 2016


I know a code review wasn't the main goal of you message but I feel
it's worth mentioning two tips:

On 22 December 2016 at 01:55, Deborah Swanson <python at deborahswanson.net> wrote:
>     ls = []
>     with open('E:\\Coding projects\\Pycharm\\Moving\\New Listings.csv',
> 'r') as infile:
>         raw = csv.reader(infile)
>         indata = list(raw)
>         rows = indata.__len__()
>     for i in range(rows):
>         ls.append(indata[i])

This block init an empty list, creates a csv.reader, processes it all
converting to a list then loops over every in this list and assign
this item to the initially created list. The initial list object is
actually useless since at the end ls and rows will contain exactly the
same objects.
Your code can be simplified with:

     with open(your_file_path) as infile:
         ls = list(csv.reader(infile))


Then I see you're looping with an index-based approach, here

>     for i in range(rows):
>         ls.append(indata[i])
[…]
>     # find & mark dups, make hyperlink if not dup
>     for i in range(1, len(ls) - 1):

and in the other functions, basically wherever you use len().

Check Ned Batchelders's "Loop like a native" talk about that, there
are both a webpage and a PyCon talk.
By using "native" looping you'll get simplified code that is more
expressive in less lines.


-- 
Andrea


More information about the Python-list mailing list