On Sun, May 8, 2016 at 11:16 AM, DFS <nos...@dfs.com> wrote: > On 5/7/2016 1:01 PM, Chris Angelico wrote: >> 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]
So you're iterating over addr lots of times, and building separate lists. As an alternative, you could iterate over it *once*, and have a single object representing an address. > 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. Because 'j' is insignificant here, as is the length of the list. What you're doing is iterating over three parallel lists - not counting numbers. Imagine that, instead of lists, you just have *sequences* - ordered collections of things. You can follow a recipe without knowing the numbers of the individual lines; you just need to know the sequence. Here, iterate over this collection: * Collect ingredients. * Cream the butter and the sugar. * Sift the salt into the flour. * Fold the mixture into an origami crane. These instructions work whether they're numbered or not. ChrisA -- https://mail.python.org/mailman/listinfo/python-list