Re: Python 3: dict dict.keys()

2013-07-25 Thread Steven D'Aprano
On Wed, 24 Jul 2013 13:17:12 -0400, Terry Reedy wrote: On 7/24/2013 12:34 PM, Chris Angelico wrote: Side point: Why is iterating over a dict equivalent to .keys() rather than .items()? It feels odd that, with both options viable, the implicit version iterates over half the dict instead of

Re: Python 3: dict dict.keys()

2013-07-25 Thread Steven D'Aprano
On Wed, 24 Jul 2013 17:59:43 -0700, Ethan Furman wrote: Repeat after me: In Python 2, d.keys() returns a list of keys, so if I want a list of keys in Python 3, call list explicitly list(d.keys()). Actually, I would recommend `list(d)`, which also works the same in both 2 and 3. Fair point.

Re: Python 3: dict dict.keys()

2013-07-25 Thread Chris Angelico
On Thu, Jul 25, 2013 at 3:48 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Dicts aren't sets, and don't support set methods: py d1 - d2 Traceback (most recent call last): File stdin, line 1, in module TypeError: unsupported operand type(s) for -: 'dict' and 'dict' I

Re: Python 3: dict dict.keys()

2013-07-25 Thread alex23
On 25/07/2013 4:31 AM, Ethan Furman wrote: 2) Hopefully learn something about when a view is useful. I haven't seeen this mentioned - forgive me if it's a repeat - but views are constant references to whichever set they represent. Python 2.7: dd = dict(a=1,b=2,c=3) keys = dd.keys()

Re: Python 3: dict dict.keys()

2013-07-25 Thread Steven D'Aprano
On Wed, 24 Jul 2013 11:31:58 -0700, Ethan Furman wrote: On 07/24/2013 10:23 AM, Stefan Behnel wrote: Peter Otten, 24.07.2013 08:23: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? To me it looks like views

Re: Python 3: dict dict.keys()

2013-07-25 Thread Steven D'Aprano
On Thu, 25 Jul 2013 16:02:42 +1000, Chris Angelico wrote: On Thu, Jul 25, 2013 at 3:48 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Dicts aren't sets, and don't support set methods: py d1 - d2 Traceback (most recent call last): File stdin, line 1, in module TypeError:

Re: Python 3: dict dict.keys()

2013-07-25 Thread Chris Angelico
On Thu, Jul 25, 2013 at 5:04 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: - Views support efficient (O(1) in the case of keys) membership testing, which neither iterkeys() nor Python2 keys() does. To save me the trouble and potential error of digging through the source code:

Re: Python 3: dict dict.keys()

2013-07-25 Thread Peter Otten
Chris Angelico wrote: On Thu, Jul 25, 2013 at 5:04 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: - Views support efficient (O(1) in the case of keys) membership testing, which neither iterkeys() nor Python2 keys() does. To save me the trouble and potential error of

Re: Python 3: dict dict.keys()

2013-07-25 Thread Chris Angelico
On Thu, Jul 25, 2013 at 5:27 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Thu, 25 Jul 2013 16:02:42 +1000, Chris Angelico wrote: On Thu, Jul 25, 2013 at 3:48 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Dicts aren't sets, and don't support set methods:

Re: Python 3: dict dict.keys()

2013-07-25 Thread Steven D'Aprano
On Thu, 25 Jul 2013 18:15:22 +1000, Chris Angelico wrote: On Thu, Jul 25, 2013 at 5:27 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Thu, 25 Jul 2013 16:02:42 +1000, Chris Angelico wrote: On Thu, Jul 25, 2013 at 3:48 PM, Steven D'Aprano

Re: Python 3: dict dict.keys()

2013-07-25 Thread Chris Angelico
On Thu, Jul 25, 2013 at 7:44 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Thu, 25 Jul 2013 18:15:22 +1000, Chris Angelico wrote: That's true, but we already have that issue with sets. What's the union of {0} and {0.0}? Python's answer: It depends on the order of the

Re: Python 3: dict dict.keys()

2013-07-25 Thread Johannes Bauer
On 25.07.2013 07:48, Steven D'Aprano wrote: Then you aren't looking very closely. d.keys() returns a set-like view into the dict, which is great for comparing elements: py d1 = dict.fromkeys([1, 2, 3, 4]) py d2 = dict.fromkeys([3, 4, 5, 6]) py d1.keys() d2.keys() # keys that are in both

Re: Python 3: dict dict.keys()

2013-07-25 Thread Ethan Furman
On 07/24/2013 11:01 PM, alex23 wrote: On 25/07/2013 4:31 AM, Ethan Furman wrote: 2) Hopefully learn something about when a view is useful. I haven't seeen this mentioned - forgive me if it's a repeat - but views are constant references to whichever set they represent. Python 2.7: dd =

Re: Python 3: dict dict.keys()

2013-07-25 Thread Ethan Furman
On 07/24/2013 10:48 PM, Steven D'Aprano wrote: On Wed, 24 Jul 2013 08:57:11 -0700, Ethan Furman wrote: My point is that in 2.x .keys() did something different from the dict, while in 3.x it appears to me that they are the same. Then you aren't looking very closely. Actually, I am. That's

Re: Python 3: dict dict.keys()

2013-07-25 Thread Steven D'Aprano
On Thu, 25 Jul 2013 20:34:23 +1000, Chris Angelico wrote: On Thu, Jul 25, 2013 at 7:44 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Thu, 25 Jul 2013 18:15:22 +1000, Chris Angelico wrote: That's true, but we already have that issue with sets. What's the union of {0} and

Re: Python 3: dict dict.keys()

2013-07-25 Thread Chris Angelico
On Fri, Jul 26, 2013 at 12:57 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: [ snip lengthy explanation of sets ] The union operator ought to be symmetrical, a ∪ b should be identical to b ∪ a, but isn't. Another leaky abstraction. Right. I agree with all your theory, which

Re: Python 3: dict dict.keys()

2013-07-25 Thread Ian Kelly
On Thu, Jul 25, 2013 at 2:13 AM, Peter Otten __pete...@web.de wrote: Chris Angelico wrote: On Thu, Jul 25, 2013 at 5:04 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: - Views support efficient (O(1) in the case of keys) membership testing, which neither iterkeys() nor

RE: Python 3: dict dict.keys()

2013-07-25 Thread Prasad, Ramit
Terry Reedy wrote: On 7/24/2013 4:34 PM, Prasad, Ramit wrote: I am still not clear on the advantage of views vs. iterators. A1: Views are iterables that can be iterated more than once. Therefore, they can be passed to a function that re-iterates its inputs, or to multiple functions.

Re: Python 3: dict dict.keys()

2013-07-25 Thread Peter Otten
Ian Kelly wrote: On Thu, Jul 25, 2013 at 2:13 AM, Peter Otten __pete...@web.de wrote: Chris Angelico wrote: On Thu, Jul 25, 2013 at 5:04 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: - Views support efficient (O(1) in the case of keys) membership testing, which neither

Re: Python 3: dict dict.keys()

2013-07-25 Thread Ethan Furman
On 07/25/2013 09:11 AM, Prasad, Ramit wrote: Terry Reedy wrote: On 7/24/2013 4:34 PM, Prasad, Ramit wrote: I am still not clear on the advantage of views vs. iterators. A1: Views are iterables that can be iterated more than once. Therefore, they can be passed to a function that re-iterates

Re: Python 3: dict dict.keys()

2013-07-25 Thread Terry Reedy
On 7/25/2013 12:21 PM, Ethan Furman wrote: On 07/25/2013 09:11 AM, Prasad, Ramit wrote: Hmm, that is a change that makes some sense to me. Does the view get updated when dictionary changes or is a new view needed? I assume the latter. Nope, the former. That is a big advantage that the

Re: Python 3: dict dict.keys()

2013-07-24 Thread Peter Otten
Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? I just grepped through /usr/lib/python3, and could not identify a single line where some_object.keys() wasn't either wrapped in a list (or set, sorted, max) call,

Re: Python 3: dict dict.keys()

2013-07-24 Thread Oscar Benjamin
On Jul 24, 2013 7:25 AM, Peter Otten __pete...@web.de wrote: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? I just grepped through /usr/lib/python3, and could not identify a single line where

Re: Python 3: dict dict.keys()

2013-07-24 Thread Neil Cerutti
On 2013-07-24, Peter Otten __pete...@web.de wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? I just grepped through /usr/lib/python3, and could not identify a single line where some_object.keys() wasn't either wrapped in a

Re: Python 3: dict dict.keys()

2013-07-24 Thread Peter Otten
Oscar Benjamin wrote: On Jul 24, 2013 7:25 AM, Peter Otten __pete...@web.de wrote: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? I just grepped through /usr/lib/python3, and could not identify a single

Re: Python 3: dict dict.keys()

2013-07-24 Thread Skip Montanaro
What do you mean? Why would you want to create a temporary list just to iterate over it explicitly or implicitly (set, sorted, max,...)? Because while iterating over the keys, he might also want to add or delete keys to/from the dict. You can't do that while iterating over them in-place. This

Re: Python 3: dict dict.keys()

2013-07-24 Thread Ian Kelly
On Tue, Jul 23, 2013 at 8:11 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Basically, views are set-like, not list-like. The keys and items views are set-like. The values view is not. -- http://mail.python.org/mailman/listinfo/python-list

Re: Python 3: dict dict.keys()

2013-07-24 Thread Ian Kelly
On Wed, Jul 24, 2013 at 8:58 AM, Skip Montanaro s...@pobox.com wrote: What do you mean? Why would you want to create a temporary list just to iterate over it explicitly or implicitly (set, sorted, max,...)? Because while iterating over the keys, he might also want to add or delete keys

Re: Python 3: dict dict.keys()

2013-07-24 Thread Ethan Furman
On 07/24/2013 05:51 AM, Oscar Benjamin wrote: On Jul 24, 2013 7:25 AM, Peter Otten __pete...@web.de mailto:pete...@web.de wrote: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? I just grepped through

Re: Python 3: dict dict.keys()

2013-07-24 Thread Oscar Benjamin
On Jul 24, 2013 2:27 PM, Peter Otten __pete...@web.de wrote: Oscar Benjamin wrote: On Jul 24, 2013 7:25 AM, Peter Otten __pete...@web.de wrote: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? I

Re: Python 3: dict dict.keys()

2013-07-24 Thread Chris Angelico
On Thu, Jul 25, 2013 at 1:57 AM, Ethan Furman et...@stoneleaf.us wrote: On 07/24/2013 05:51 AM, Oscar Benjamin wrote: What do you mean? Why would you want to create a temporary list just to iterate over it explicitly or implicitly (set, sorted, max,...)? You wouldn't. But you don't need

Re: Python 3: dict dict.keys()

2013-07-24 Thread Terry Reedy
On 7/24/2013 12:34 PM, Chris Angelico wrote: Side point: Why is iterating over a dict equivalent to .keys() rather than .items()? It feels odd that, with both options viable, the implicit version iterates over half the dict instead of all of it. Obviously it can't be changed now, even if

Re: Python 3: dict dict.keys()

2013-07-24 Thread Stefan Behnel
Chris Angelico, 24.07.2013 18:34: On Thu, Jul 25, 2013 at 1:57 AM, Ethan Furman wrote: On 07/24/2013 05:51 AM, Oscar Benjamin wrote: What do you mean? Why would you want to create a temporary list just to iterate over it explicitly or implicitly (set, sorted, max,...)? You wouldn't. But

Re: Python 3: dict dict.keys()

2013-07-24 Thread Stefan Behnel
Peter Otten, 24.07.2013 08:23: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? I just grepped through /usr/lib/python3, and could not identify a single line where some_object.keys() wasn't either wrapped in a

Re: Python 3: dict dict.keys()

2013-07-24 Thread Chris Angelico
On Thu, Jul 25, 2013 at 3:17 AM, Terry Reedy tjre...@udel.edu wrote: On 7/24/2013 12:34 PM, Chris Angelico wrote: Side point: Why is iterating over a dict equivalent to .keys() rather than .items()? It feels odd that, with both options viable, the implicit version iterates over half the dict

Re: Python 3: dict dict.keys()

2013-07-24 Thread Ethan Furman
On 07/24/2013 10:23 AM, Stefan Behnel wrote: Peter Otten, 24.07.2013 08:23: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? To me it looks like views are a solution waiting for a problem. They reduce the API

Re: Python 3: dict dict.keys()

2013-07-24 Thread Stefan Behnel
Ethan Furman, 24.07.2013 20:31: On 07/24/2013 10:23 AM, Stefan Behnel wrote: Peter Otten, 24.07.2013 08:23: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? To me it looks like views are a solution waiting for

Re: Python 3: dict dict.keys()

2013-07-24 Thread Ethan Furman
On 07/24/2013 12:59 PM, Stefan Behnel wrote: I think the question is: how else would you implement an interface that doesn't restrict itself to returning a list? I mean, previously, the following was totally inefficient in terms of memory: value in d.values() It now avoids creating an

RE: Python 3: dict dict.keys()

2013-07-24 Thread Prasad, Ramit
Stefan Behnel wrote: Ethan Furman, 24.07.2013 20:31: On 07/24/2013 10:23 AM, Stefan Behnel wrote: Peter Otten, 24.07.2013 08:23: Ethan Furman wrote: So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? To me it looks like

Re: Python 3: dict dict.keys()

2013-07-24 Thread Christian Heimes
Am 24.07.2013 18:34, schrieb Chris Angelico: Side point: Why is iterating over a dict equivalent to .keys() rather than .items()? It feels odd that, with both options viable, the implicit version iterates over half the dict instead of all of it. Obviously it can't be changed now, even if

Re: Python 3: dict dict.keys()

2013-07-24 Thread Terry Reedy
On 7/24/2013 4:34 PM, Prasad, Ramit wrote: I am still not clear on the advantage of views vs. iterators. A1: Views are iterables that can be iterated more than once. Therefore, they can be passed to a function that re-iterates its inputs, or to multiple functions. They support 'x in view'

Re: Python 3: dict dict.keys()

2013-07-24 Thread Ethan Furman
On 07/24/2013 01:34 PM, Prasad, Ramit wrote: I am still not clear on the advantage of views vs. iterators. What makes d.viewkeys() better than d.iterkeys()? Why did they decide not to rename d.iterkeys() to d.keys() and instead use d.viewkeys()? Is the iteration over a set operation on keys

Re: Python 3: dict dict.keys()

2013-07-24 Thread Ethan Furman
On 07/23/2013 07:11 PM, Steven D'Aprano wrote: On Tue, 23 Jul 2013 18:16:08 -0700, Ethan Furman wrote: So now, in Python 3, .keys(), .values(), even .items() all return these 'view' thingies. And everything I thought I knew about when to use one or the other went out the window. Surely not.

Re: Python 3: dict dict.keys()

2013-07-24 Thread Ben Finney
Ethan Furman et...@stoneleaf.us writes: On 07/23/2013 07:11 PM, Steven D'Aprano wrote: On Tue, 23 Jul 2013 18:16:08 -0700, Ethan Furman wrote: And everything I thought I knew about when to use one or the other went out the window. Surely not. The fundamental behaviour of Python's data

Re: Python 3: dict dict.keys()

2013-07-24 Thread Steven D'Aprano
On Wed, 24 Jul 2013 08:57:11 -0700, Ethan Furman wrote: My point is that in 2.x .keys() did something different from the dict, while in 3.x it appears to me that they are the same. Then you aren't looking very closely. d.keys() returns a set-like view into the dict, which is great for

Re: Python 3: dict dict.keys()

2013-07-23 Thread Steven D'Aprano
On Tue, 23 Jul 2013 18:16:08 -0700, Ethan Furman wrote: Back in Python 2.x days I had a good grip on dict and dict.keys(), and when to use one or the other. Then Python 3 came on the scene with these things called 'views', and while range couldn't be bothered, dict jumped up and down