Using namedtuples field names for column indices in a list of lists

Deborah Swanson python at deborahswanson.net
Mon Jan 9 16:56:04 EST 2017


Tim Chase wrote, on January 09, 2017 6:22 AM
> 
> On 2017-01-08 22:58, Deborah Swanson wrote:
> > 1) I have a section that loops through the sorted data, compares two

> > adjacent rows at a time, and marks one of them for deletion if the 
> > rows are identical.
> > and my question is whether there's a way to work with two adjacent 
> > rows without using subscripts?
> 
> I usually wrap the iterable in something like
> 
>   def pairwise(it):
>     prev = next(it)
>     for thing in it:
>       yield prev, thing
>       prev = thing
> 
>   for prev, cur in pairwise(records):
>     compare(prev, cur)
> 
> which I find makes it more readable.
> 
> -tkc

This looks very useful, and comparing two adjacent rows is something I
do often. Thanks Tim!

Deborah




More information about the Python-list mailing list