Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51149

Re: Python 3: dict & dict.keys()

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.020
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'explicitly': 0.05; 'subject:Python': 0.06; 'iterate': 0.09; 'oh,': 0.09; 'skip:/ 10': 0.09; 'subject:()': 0.09; 'wrapped': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '&gt;&gt;': 0.16; '24,': 0.16; 'benjamin': 0.16; 'iterated': 0.16; 'set,': 0.16; 'to:addr:web.de': 0.16; '\xa0what': 0.16; 'wrote:': 0.18; 'code.': 0.18; '>>>': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; '&gt;&gt;&gt;': 0.24; '2.x': 0.24; 'either.': 0.24; 'earlier': 0.24; 'looks': 0.24; '(or': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; '&gt;': 0.26; 'am,': 0.29; 'forgot': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'agreed.': 0.31; 'keys': 0.31; 'once,': 0.31; 'lists': 0.32; 'skip:m 30': 0.32; 'guess': 0.33; 'actual': 0.34; 'noticed': 0.34; 'maybe': 0.34; 'could': 0.34; 'problem.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'picking': 0.36; "didn't": 0.36; 'so,': 0.37; 'list': 0.37; 'pm,': 0.38; 'rather': 0.38; 'sure': 0.39; 'either': 0.39; 'users': 0.40; 'how': 0.40; 'even': 0.60; 'most': 0.60; 'identify': 0.61; 'no.': 0.61; 'different': 0.65; 'temporary': 0.65; 'useful.': 0.68; 'subject: & ': 0.68; 'jul': 0.74; 'saw': 0.77; '7:25': 0.84; 'ethan': 0.84; 'furman': 0.84; 'idiom': 0.84; 'lying': 0.84; 'oscar': 0.84; 'mean.': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:cc:content-type; bh=RW4LLudtZTTYnwkMAaCyHJF9HwONEg12zoA0RJ+YqYw=; b=CYMXvf6x8gmVyJEKZzhztBH4WRe63r7AtUnt7C4+sBvOa4Me98cyLnqpWt6YyALxzK 4LBXycgz4OUyEqxUMHlIurmgRvMlXW7nga6Nlvjq7x9b7Axj0Q85f2SZsn1wmrpBij+C u/YYxkx5m/o4ii6vzz9wHKKZX1g2G7s+Uw775UmNcNzTlBjKm1zAtvACL9V7OlO/YfAs sr2Ehc/tM5C8mRnEYRr4vLTgPZ39AsvVfSnXp6Zsg24x1U5HIKwKwrkaH4RD6O03sWmy IK+kB6jtnTPZo4HNDgTv/DS+GTyFmIjTeE2rxo8NY5pmZClXITyctT7+meJIaAcekDTG hasA==
MIME-Version 1.0
X-Received by 10.66.231.3 with SMTP id tc3mr44911836pac.167.1374683521097; Wed, 24 Jul 2013 09:32:01 -0700 (PDT)
Date Wed, 24 Jul 2013 17:32:01 +0100
Subject Re: Python 3: dict & dict.keys()
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
To Peter Otten <__peter__@web.de>
Content-Type multipart/alternative; boundary=047d7b15ab5df426f304e24472c4
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5051.1374683530.3114.python-list@python.org> (permalink)
Lines 132
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374683530 news.xs4all.nl 15911 [2001:888:2000:d::a6]:48361
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51149

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Jul 24, 2013 2:27 PM, "Peter Otten" <__peter__@web.de> wrote:
>
> Oscar Benjamin wrote:
>
> > On Jul 24, 2013 7:25 AM, "Peter Otten" <__peter__@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 some_object.keys() wasn't either wrapped in a list (or set,
> >> sorted, max) call, or iterated over.
> >>
> >> To me it looks like views are a solution waiting for a problem.
> >
> > What do you mean? Why would you want to create a temporary list just to
> > iterate over it explicitly or implicitly (set, sorted, max,...)?
>
> I mean I don't understand the necessity of views when all actual usecases
> need iterators. The 2.x iterkeys()/iteritems()/itervalues() methods didn't
> create lists either.

Oh, okay. I see what you mean.

>
> Do you have 2.x code lying around where you get a significant advantage by
> picking some_dict.viewkeys() over some_dict.iterkeys()?

No. I don't think I've ever used viewkeys. I noticed it once, didn't see an
immediate use and forgot about it but...

> I could construct
> one
>
> >>> d = dict(a=1, b=2, c=3)
> >>> e = dict(b=4, c=5, d=6)
> >>> d.viewkeys() & e.viewkeys()
> set(['c', 'b'])

that might be useful.

>
> but have not seen it in the wild.

> My guess is that most non-hardcore users don't even know about viewkeys().
> By the way, my favourite idiom to iterate over the keys in both Python 2
and
> 3 is -- for example -- max(some_dict) rather than
> max(some_dict.whateverkeys()).

Agreed.

Earlier I saw that I had list(some_dict) in some code. Not sure why but
maybe because it's the same in Python 2 and 3.

Oscar

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python 3: dict & dict.keys() Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-24 17:32 +0100

csiph-web