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


Groups > comp.lang.python > #99308

Re: Returning a result from 3 items in a list

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Returning a result from 3 items in a list
Date 2015-11-24 11:32 +0100
Organization None
Message-ID <mailman.99.1448361186.2291.python-list@python.org> (permalink)
References <133829df-f57f-442f-9dbf-b22b22f656ae@googlegroups.com>

Show all headers | View raw


Cai Gengyang wrote:

> Here's a dictionary with 3 values :
> 
> results = {
>   "gengyang": 14,
>   "ensheng": 13,
>   "jordan": 12
> }
> 
> How do I define a function that takes the last of the 3 items in that list
> and returns Jordan's results i.e. (12) ?
> 
> Thanks a lot !

You can access the last item in a *list* with items[-1]:

>>> results = [
... ("gengyang", 14),
... ("ensheng", 13),
... ("jordan", 12)
... ]
>>> results[-1]
('jordan', 12)

A *dict* is unordered (the order is undefined, to be exact), so there is no 
"last" item. For the rare case when you need both order and fast lookup 
there's collections.OrderedDict:

>>> results = collections.OrderedDict([
... ("gengyang", 14),
... ("ensheng", 13),
... ("jordan", 12)
... ])

Get the last value non-destructively:

>>> results[next(reversed(results))] # is there a less tedious way?
12

Get the last pair, removing it from the dictionary:

>>> results.popitem(last=True)
('jordan', 12)
>>> "jordan" in results
False

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


Thread

Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 02:04 -0800
  Re: Returning a result from 3 items in a list Chris Angelico <rosuav@gmail.com> - 2015-11-24 21:14 +1100
    Re: Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 02:27 -0800
      Re: Returning a result from 3 items in a list Peter Otten <__peter__@web.de> - 2015-11-24 11:35 +0100
        Re: Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 02:47 -0800
      Re: Returning a result from 3 items in a list Chris Angelico <rosuav@gmail.com> - 2015-11-24 22:05 +1100
        Re: Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-24 03:06 -0800
  Re: Returning a result from 3 items in a list Peter Otten <__peter__@web.de> - 2015-11-24 11:32 +0100
  Re: Returning a result from 3 items in a list Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-24 14:07 +0000
    Re: Returning a result from 3 items in a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-11-24 14:28 +0000
      Re: Returning a result from 3 items in a list Ned Batchelder <ned@nedbatchelder.com> - 2015-11-24 08:59 -0800
        Re: Returning a result from 3 items in a list Cai Gengyang <gengyangcai@gmail.com> - 2015-11-25 02:26 -0800
          Re: Returning a result from 3 items in a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-11-25 11:22 +0000

csiph-web