pylint woes

DFS nospam at dfs.com
Sat May 7 21:16:25 EDT 2016


On 5/7/2016 1:01 PM, Chris Angelico wrote:
> On Sun, May 8, 2016 at 2:51 AM, DFS <nospam at dfs.com> wrote:
>> [1]
>> pylint says "Consider using enumerate instead of iterating with range and
>> len"
>>
>> the offending code is:
>> for j in range(len(list1)):
>>   do something with list1[j], list2[j], list3[j], etc.
>>
>> enumeration would be:
>> for j,item in enumerate(list1):
>>   do something with list1[j], list2[j], list3[j], etc.
>>
>> Is there an advantage to using enumerate() here?
>
> The suggestion from a human would be to use zip(), or possibly to
> change your data structures.

Happens like this:

address data is scraped from a website:

names = tree.xpath()
addr  = tree.xpath()

I want to store the data atomically, so I parse street, city, state, and 
zip into their own lists.

"1250 Peachtree Rd, Atlanta, GA 30303

street = [s.split(',')[0] for s in addr]
city   = [c.split(',')[1].strip() for c in addr]
state  = [s[-8:][:2] for s in addr]
zipcd  = [z[-5:] for z in addr]

names  = ["Taco Bell", "Wendy's"]
addr   = ['928 Buford Dr, Tucker, GA 30043', '4880 Ptree Pkwy, Atlanta, 
GA 30303']
street = ['928 Buford Dr', '4880 Sugarloaf Pkwy']
city   = ['Tucker','Atlanta']
state  = ['GA','GA']
zipcd  = ['30043','30303']


When you say 'possibly change data structures'... to what?



> for item1, item2, item3 in zip(list1, list2, list3):
>     do something with the items

ziplists = zip(names,street,city,state,zipcd)
print ziplists

[('Taco Bell', '928 Buford Dr', 'Tucker', 'GA', '30043'),
  ("Wendy's", '4880 Sugarloaf Pkwy', 'Atlanta', 'GA', '30303')]



Why is it better to zip() them up and use:

for item1, item2, item3 in zip(list1, list2, list3):
      do something with the items

than

for j in range(len(list1)):
    do something with list1[j], list2[j], list3[j], etc.




More information about the Python-list mailing list