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


Groups > comp.lang.python > #101554

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
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 <mailman.71.1452618384.13488.python-list@python.org> (permalink)
References <f2711b57-7b11-4fd5-8bab-a3e8581177b5@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de vv6/A8QCbpKNWOu2j0kWFAWHFzexyahebYYxlbKvJImQ==
Return-Path <rosuav@gmail.com>
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 <f2711b57-7b11-4fd5-8bab-a3e8581177b5@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:101554

Show key headers only | View raw


On Wed, Jan 13, 2016 at 3:50 AM, Nick Mellor <thebalancepro@gmail.com> 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 "<input>", line 1, in <module>
> 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

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


Thread

subscripting Python 3 dicts/getting the only value in a Python 3 dict Nick Mellor <thebalancepro@gmail.com> - 2016-01-12 08:50 -0800
  RE: subscripting Python 3 dicts/getting the only value in a Python 3 dict Emanuel Barry <vgr255@live.ca> - 2016-01-12 12:00 -0500
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Chris Angelico <rosuav@gmail.com> - 2016-01-13 04:06 +1100
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Terry Reedy <tjreedy@udel.edu> - 2016-01-12 12:12 -0500
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-12 15:18 -0200
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Peter Otten <__peter__@web.de> - 2016-01-12 18:32 +0100
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-12 10:39 -0700
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-01-12 19:47 +0200
    Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Marko Rauhamaa <marko@pacujo.net> - 2016-01-12 21:42 +0200
      Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-01-12 22:18 +0200
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Peter Otten <__peter__@web.de> - 2016-01-12 19:09 +0100
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-12 16:48 -0200
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Peter Otten <__peter__@web.de> - 2016-01-12 19:59 +0100
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-12 17:12 -0200
    Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Steven D'Aprano <steve@pearwood.info> - 2016-01-13 12:29 +1100
      Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-13 13:53 -0200
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Steven D'Aprano <steve@pearwood.info> - 2016-01-13 12:23 +1100
    Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Grant Edwards <invalid@invalid.invalid> - 2016-01-13 15:58 +0000

csiph-web