Re: [Tutor] basic lists and loops question

2008-05-14 Thread Jon Crump
Kent, On Wed, 14 May 2008, Kent Johnson wrote: I understand about removing elements from a container you're iterating. Is data.remove(x) problematic in this context? Yes. It can cause the iteration to skip elements ofthe list. Better to post-process the list with a list comprehension: evts = [

Re: [Tutor] basic lists and loops question

2008-05-14 Thread Kent Johnson
On Wed, May 14, 2008 at 9:06 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: >> I understand about removing elements from a container you're iterating. Is >> data.remove(x) problematic in this context? > > Yes. It can cause the iteration to skip elements of the list. For example: In [1]: l=range(5) I

Re: [Tutor] basic lists and loops question

2008-05-14 Thread Kent Johnson
On Wed, May 14, 2008 at 6:03 PM, Jon Crump <[EMAIL PROTECTED]> wrote: > def events(data): > evts = [] > for x in data: >for y in data: > if (x['placename'] == y['placename']) and (x['end'].month + 1 == > y['start'].month) and (y['start'] - x['end'] == datetime.timedelta(1)): >x['

Re: [Tutor] basic lists and loops question

2008-05-14 Thread Jon Crump
Bob, and Kent, Many thanks! Sounds like the key 'processed' is created by the assignment x['processed'] = True. So those dictionaries that have not experienced this assignment have no such key. You should instead use: if 'processed' in x: Doh! Now that WAS obvious Try lst.remove(x) Now t

Re: [Tutor] basic lists and loops question

2008-05-14 Thread Kent Johnson
On Wed, May 14, 2008 at 3:02 PM, Jon Crump <[EMAIL PROTECTED]> wrote: > Something basic about lists and loops that I'm not getting here. > I have a function that searches through them to find pairs of dictionaries > that satisfy certain criteria. When the nested loops find such a pair, I > need to

Re: [Tutor] basic lists and loops question

2008-05-14 Thread bob gailer
Jon Crump wrote: Something basic about lists and loops that I'm not getting here. I've got a long list of dictionaries that looks like this: lst = [{'placename': u'Stow, Lincolnshire', 'long-name': u'Stow, Lincolnshire.', 'end': datetime.date(1216, 9, 28), 'start': datetime.date(1216, 9, 26)}

[Tutor] basic lists and loops question

2008-05-14 Thread Jon Crump
Something basic about lists and loops that I'm not getting here. I've got a long list of dictionaries that looks like this: lst = [{'placename': u'Stow, Lincolnshire', 'long-name': u'Stow, Lincolnshire.', 'end': datetime.date(1216, 9, 28), 'start': datetime.date(1216, 9, 26)}, {'placename': u'