Path: csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Finding scores from a list Date: Tue, 24 Nov 2015 14:50:01 +0100 Organization: None Lines: 37 Message-ID: References: <277843f7-c898-4378-85ea-841b09a289e3@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de v/mpQMKokRM/8JJrbJUzvwJLz18lkQVElDlPucDut5pQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'indices': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'typeerror:': 0.09; 'python': 0.10; 'integers,': 0.16; 'item:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'first,': 0.20; '"",': 0.22; 'matching': 0.23; 'tried': 0.24; '(most': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'container': 0.29; 'str': 0.29; 'traceback': 0.33; 'file': 0.34; 'this?': 0.34; 'list': 0.34; 'text': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'thank': 0.38; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'provide': 0.61; 'note:': 0.66; 'results': 0.66; 'picture.': 0.84; 'score.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9007.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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:99332 Cai Gengyang wrote: > > results = [ > {"id": 1, "name": "ensheng", "score": 10}, > {"id": 2, "name": "gengyang", "score": 12}, > {"id": 3, "name": "jordan", "score": 5}, > ] > > I want to find gengyang's score. This is what I tried : > >>>> print((results["gengyang"])["score"]) > > but I got an error message instead : > > Traceback (most recent call last): > File "", line 1, in > print((results["gengyang"])["score"]) > TypeError: list indices must be integers, not str > > Any ideas how to solve this? Thank you .. As the outer container is a list you have to provide an index: results[1]["score"] You can also search for a matching item: for result in results: if result["name"] == "gengyang": print(result["score"]) but when the list grows performance will suffer. General note: you will have a better experience learning Python when you read some introductory text first, to get the big picture.