Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:Python': 0.06; 'attribute': 0.07; 'modify': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'subject:()': 0.09; 'window.': 0.09; '~ethan~': 0.09; 'python': 0.11; 'dict': 0.16; 'grip': 0.16; 'iterating': 0.16; 'iteration': 0.16; 'jumped': 0.16; 'some)': 0.16; 'appropriate': 0.16; 'header:User-Agent:1': 0.23; '2.x': 0.24; 'question': 0.24; 'went': 0.31; '"",': 0.31; '(maybe': 0.31; 'keys': 0.31; 'file': 0.32; '(most': 0.33; 'maybe': 0.34; 'something': 0.35; 'right?': 0.36; 'doing': 0.36; 'example,': 0.37; 'so,': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; "couldn't": 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'called': 0.40; 'how': 0.40; 'days': 0.60; 'even': 0.60; 'deleting': 0.60; 'range': 0.61; 'received:173': 0.61; 'back': 0.62; 'different': 0.65; 'benefit': 0.68; 'received:69.56': 0.68; 'subject: & ': 0.68; 'other.': 0.75; "'view'": 0.84; 'shouting,': 0.84; "'remove'": 0.91; 'received:gateway02.websitewelcome.com': 0.91; 'scene': 0.93 Date: Tue, 23 Jul 2013 18:16:08 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: Python Subject: Python 3: dict & dict.keys() Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:43469 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374628577 news.xs4all.nl 15951 [2001:888:2000:d::a6]:41683 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:51114 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 shouting, "I want some!" 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. For example, if you need to modify a dict while iterating over it, use .keys(), right? Wrong: --> d = {1: 'one', 2:'two', 3:'three'} --> for k in d.keys(): ... if k == 1: ... del d[k] ... Traceback (most recent call last): File "", line 1, in RuntimeError: dictionary changed size during iteration If you need to manipulate the keys (maybe adding some, maybe deleting some) before doing something else with final key collection, use .keys(), right? Wrong: --> dk = d.keys() --> dk.remove(2) Traceback (most recent call last): File "", line 1, in AttributeError: 'dict_keys' object has no attribute 'remove' I understand that the appropriate incantation in Python 3 is: --> for k in list(d) ... ... or --> dk = list(d) --> dk.remove(2) which also has the added benefit of working the same way in Python 2. So, my question boils down to: in Python 3 how is dict.keys() different from dict? What are the use cases? -- ~Ethan~