Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Date: Wed, 13 Jan 2016 04:06:20 +1100 Lines: 30 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de vv6/A8QCbpKNWOu2j0kWFAWHFzexyahebYYxlbKvJImQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Python': 0.05; 'indexing': 0.07; 'subject:getting': 0.07; 'cc:addr:python-list': 0.09; 'item,': 0.09; 'typeerror:': 0.09; 'python': 0.10; 'jan': 0.11; 'wed,': 0.15; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nick': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '"",': 0.22; 'am,': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; '13,': 0.29; 'dictionary': 0.29; "i'm": 0.30; 'subject:/': 0.30; 'code': 0.30; "i'd": 0.31; 'common': 0.33; 'surprised': 0.33; 'traceback': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'could': 0.35; "isn't": 0.35; 'item': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209': 0.38; 'end': 0.39; 'whatever': 0.39; 'does': 0.39; 'subject:the': 0.39; 'where': 0.40; 'is.': 0.63; 'chrisa': 0.84; 'subject:value': 0.84; 'to:none': 0.91; 'write:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=pwvWm9UiC3/TdB87qsHT7zExKQXB/PgFj7JiRfCniyc=; b=pQVYZbaC7xjqWuHX0bopfU/ttiqdFXAuBNCtRUGVkzhVRP8G7IhV/vEl5njRx8ZF/f 6SmbdfYfcYNNZZiD9MdYPSJRMiWwBdCAiRRitRneulN9r39MzoAbhenVjr0AFPNAxr+u qWg+ceAhz5kwYOE/b3MfEQ66qWB23g14JNeCi+1NkqkLi/0671tR+cQoCylqJsw9QWS3 nHhm6O6Eo9JBERY+mAgFhbc6CDoVZPWiFuoNtOEhiRY1z70T9L3cCTardp3S2Thll873 CKOFxh+bt8SLMka6mAJFwzIYf1WH1gTPM0PiRvMyueHRpyGHQHD2FVVcKSM4xE4NyW8M 51Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=pwvWm9UiC3/TdB87qsHT7zExKQXB/PgFj7JiRfCniyc=; b=ZnSOikraLZ6Pq94qeR+mOGxiGhUlQu9KpKEO+/IvlOVxSHtErb9x6dG/Y1Z1lbZ1ch q/B2JTobTmN0qEADkPyKWTZQHOkMNqxd+hBJv5DufSBcLPR9H2oCIzZCA9yC/tQZAr5Q X11VjPAOh8YDSLGzCVYetjqyV4WUp7VpsDhMpXpG/yS2E/l+8rNMZ8CO93iOxbDDmYfz J2sp21LGlgiLSF4JfRncSJm4ia2Z4eX1NJzS0aYwbwRNWbySpF2LiGtmehwhKDIcU2t6 cq+CHntVQAHccG7wEzWd1b5a3pHeyL24VwVHD7zLQaar128i7HwvqvStPafK9UmCJqAD FmbA== X-Gm-Message-State: ALoCoQnpsyGDzBoAGbTfFidxnu2PR+gyWQknrAJtjS69PRaHLGxS+CX8S7eiRnmPezImD2h72K2yOUnDmbaGD0qyStBy85QVMg== X-Received: by 10.107.14.73 with SMTP id 70mr89878276ioo.31.1452618380632; Tue, 12 Jan 2016 09:06:20 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101554 On Wed, Jan 13, 2016 at 3:50 AM, Nick Mellor wrote: > There is a case in my code where I know a dictionary has only one item in it. I want to get the value of that item, whatever the key is. > > In Python2 I'd write: > >>>> d = {"Wilf's Cafe": 1} >>>> d.values()[0] > 1 > > and that'd be an end to it. > > In Python 3: > >>>> d = {"Wilf's Cafe": 1} >>>> d.values()[0] > Traceback (most recent call last): > File "", line 1, in > TypeError: 'dict_values' object does not support indexing > "Wilf's Cafe" >>>> d[list(d)[0]] > 1 You could try: next(iter(d.values())) but honestly, this isn't all that common a situation, so I'm not surprised there's no really simple and clean way to do it. ChrisA