Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-05 Thread Steven D'Aprano
On 05/02/13 22:27, Oscar Benjamin wrote: On 5 February 2013 03:56, eryksun wrote: On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel wrote: Nope, in both Python 2 and 3 iterating over a dict directly just provides the key. That's also how "if key in dict" works. A dict implements __contains__ for a

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-05 Thread eryksun
On Tue, Feb 5, 2013 at 6:27 AM, Oscar Benjamin wrote: > > I almost wrote this response but then I realised that Dave probably > meant that "obj in dict" returns True if the dict has a key equal to > obj rather than if the dict has a (key, value) pair equal to obj. Thanks, that's probably what Ste

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-05 Thread Oscar Benjamin
On 5 February 2013 03:56, eryksun wrote: > On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel wrote: >>> Nope, in both Python 2 and 3 iterating over a dict directly just >>> provides the key. That's also how "if key in dict" works. > > A dict implements __contains__ for an efficient "in" test. In general

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread eryksun
On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel wrote: >> Nope, in both Python 2 and 3 iterating over a dict directly just >> provides the key. That's also how "if key in dict" works. A dict implements __contains__ for an efficient "in" test. In general, the interpreter falls back to using iteration i

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Dave Angel
On 02/04/2013 06:18 PM, Steven D'Aprano wrote: On 05/02/13 09:26, Dave Angel wrote: Another point. I don't currently have Python 3.x installed, but I seem to remember that in Python 3 you can use the dict itself as an iterator providing both key and value. If I'm right, then it could be simplif

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Steven D'Aprano
On 05/02/13 09:26, Dave Angel wrote: Another point. I don't currently have Python 3.x installed, but I seem to remember that in Python 3 you can use the dict itself as an iterator providing both key and value. If I'm right, then it could be simplified further to: for i, (k, v) in enumerate(dat

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Dave Angel
On 02/04/2013 12:58 PM, Modulok wrote: Hmm.. no kidding. Well, at least I knew I was over-complicating it. Cheers! -Modulok- Please don't top-post. Another point. I don't currently have Python 3.x installed, but I seem to remember that in Python 3 you can use the dict itself as an iterator

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Modulok
Hmm.. no kidding. Well, at least I knew I was over-complicating it. Cheers! -Modulok- On 2/4/13, Dave Angel wrote: > On 02/04/2013 12:13 PM, Modulok wrote: >> List, >> >> Simple question: Is there a common pattern for iterating a dict, but also >> providing access to an iteration counter? Here'

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Dave Angel
On 02/04/2013 12:13 PM, Modulok wrote: List, Simple question: Is there a common pattern for iterating a dict, but also providing access to an iteration counter? Here's what I usually do (below). I'm just wondering if there are other, more clever ways:: data = {'a': "apple", 'b': "banana",

[Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Modulok
List, Simple question: Is there a common pattern for iterating a dict, but also providing access to an iteration counter? Here's what I usually do (below). I'm just wondering if there are other, more clever ways:: data = {'a': "apple", 'b': "banana", 'c': "cherry"} i = 0 for k,v in da