Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
On 5 February 2013 03:56, eryksun eryk...@gmail.com wrote: On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel da...@davea.name 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 if a type lacks __contains__. 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. Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
On Tue, Feb 5, 2013 at 6:27 AM, Oscar Benjamin oscar.j.benja...@gmail.com 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 Steven meant. It's keeping item in a_dict consistent with item in list(a_dict). ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
On 05/02/13 22:27, Oscar Benjamin wrote: On 5 February 2013 03:56, eryksuneryk...@gmail.com wrote: On Mon, Feb 4, 2013 at 7:04 PM, Dave Angelda...@davea.name 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 if a type lacks __contains__. 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. It was actually me, not Dave, and yes, that's what I meant. I didn't mean that dict containment tests were literally implemented by iterating over the keys checking each one in turn, since that would be horribly inefficient for something so critical as a dict. Although I can see why eryksun may have thought so, sorry for any confusion caused by my poor wording. Although note that Python does fallback on iteration for containment if you don't define a __contains__ method: py class Test(object): ... def __getitem__(self, n): ... if n = 5: raise IndexError ... return n + 100 ... py t = Test() py 3 in t False py 103 in t True -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Iterating a dict with an iteration counter? How would *you* do it?
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 data.items(): print(i: %s, k: %s, v: %s % (i,k,v)) i += 1 Another variant, same idea:: data = {'a': apple, 'b': banana, 'c': cherry} for i,k,v in zip(range(len(data)), data.keys(), data.values()): print(i: %s, k: %s, v: %s % (i,k,v)) How would you do it? -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
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, 'c': cherry} i = 0 for k,v in data.items(): print(i: %s, k: %s, v: %s % (i,k,v)) i += 1 Another variant, same idea:: data = {'a': apple, 'b': banana, 'c': cherry} for i,k,v in zip(range(len(data)), data.keys(), data.values()): print(i: %s, k: %s, v: %s % (i,k,v)) How would you do it? -Modulok- enumerate() for i, (k, v) in enumerate(data.items()): -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
Hmm.. no kidding. Well, at least I knew I was over-complicating it. Cheers! -Modulok- On 2/4/13, Dave Angel da...@davea.name 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'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 data.items(): print(i: %s, k: %s, v: %s % (i,k,v)) i += 1 Another variant, same idea:: data = {'a': apple, 'b': banana, 'c': cherry} for i,k,v in zip(range(len(data)), data.keys(), data.values()): print(i: %s, k: %s, v: %s % (i,k,v)) How would you do it? -Modulok- enumerate() for i, (k, v) in enumerate(data.items()): -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
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 providing both key and value. If I'm right, then it could be simplified further to: for i, (k, v) in enumerate(data): A simple test will prove me right or wrong. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
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(data): 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. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
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 simplified further to: for i, (k, v) in enumerate(data): 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. Then I'm glad I was tentative about it. I do recall there was some difference. Was it just that items(), keys() and values() methods return a view (iterator) instead of a list, and the iter*() versions are gone? -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?
On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel da...@davea.name 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 if a type lacks __contains__. In 2.x iter(some_dict) returns a dictionary-keyiterator (weird hyphen). In 3.x it's a dict_keyiterator (normal underscore). Was it just that items(), keys() and values() methods return a view (iterator) instead of a list, and the iter*() versions are gone? In 3.x, keys() and items() return views that are iterable (__iter__) and that implement the sequence methods __len__ and __contains__ as well as a few set operations that return a set: intersection (), union (|), difference (-), and symmetric difference (^). Using the set methods for items() requires the values to also be hashable. The view returned by values() doesn't bother implementing __contains__ and the set operations, but it does have __iter__ and __len__. 2.7 provides these views via viewkeys(), viewitems(), and viewvalues(). The corresponding iterators returned by iter() in 3.x are dict_keyiterator, dict_itemiterator, and dict_valueiterator. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor