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

Deborah Swanson python at deborahswanson.net
Mon Jan 9 05:45:54 EST 2017


Antoon Pardon wrote, on January 09, 2017 2:14 AM

> > 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.
> >
> > I'm using
> >
> > for i in range(len(records)-1):
> >     r1 = records[i]
> >     r2 = records[i+1]
> >     if r1.xx = r2.xx:
> > 		.
> > 		.
> > and my question is whether there's a way to work with two adjacent 
> > rows without using subscripts?
> 
> for r1, r2 in zip(records[i], records[i+1]):
>     if r1.xx == r2.xx
>         .
>         .

Ok, I've seen the zip function before and it might do the job, but here
I think you're suggesting:

for i in range(len(records)-1):
	for r1, r2 in zip(records[i], records[i+1]):
		if r1.xx == r2.xx
			.
			.

The hope was to do the loop without subscripts, and this may or may not
have gotchas because records is a namedtuple:

for r in records(1:):
	.
	.

Deborah



More information about the Python-list mailing list