Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106177
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Terry Reedy <tjreedy@udel.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: Slice equivalent to dict.get |
| Date | Thu, 31 Mar 2016 13:51:24 -0400 |
| Lines | 69 |
| Message-ID | <mailman.274.1459446705.28225.python-list@python.org> (permalink) |
| References | <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com> <ndjfg3$b3k$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de lEZDsCAZerU7mxuWZkJnBwMkAxyd7IwvmL6mRK6nNXpQ== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '-1)': 0.07; 'exception.': 0.07; 'raises': 0.07; "'default'": 0.09; '-1,': 0.09; 'default:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'jan': 0.11; 'index': 0.13; 'def': 0.13; "'a',": 0.16; "'c'": 0.16; '16]': 0.16; '[2,': 0.16; 'indexerror:': 0.16; 'range,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'sequence:': 0.16; 'slice.': 0.16; 'wrote:': 0.16; 'helper': 0.18; 'try:': 0.18; '"",': 0.22; 'function:': 0.22; 'tuples': 0.22; 'am,': 0.23; '(most': 0.24; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'equivalent': 0.27; 'index,': 0.29; 'skip:[ 10': 0.31; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'wrap': 0.33; 'file': 0.34; 'except': 0.34; 'lists': 0.34; 'list': 0.34; 'replace': 0.35; "isn't": 0.35; 'sometimes': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'enough': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'default': 0.61; 'received:96': 0.63; 'note:': 0.66; 'god': 0.67; 'otten': 0.84; 'received:fios.verizon.net': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | pool-96-227-207-81.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.1 |
| In-Reply-To | <ndjfg3$b3k$1@ger.gmane.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| 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:106177 |
Show key headers only | View raw
On 3/31/2016 11:24 AM, Peter Otten wrote:
> Steven D'Aprano wrote:
>
>> Sometimes people look for a method which is equivalent to dict.get, where
>> they can set a default value for when the key isn't found:
>>
>>
>> py> d = {1: 'a', 2: 'b'}
>> py> d.get(999, '?')
>> '?'
>>
>>
>> The equivalent for sequences such as lists and tuples is a slice. If the
>> slice is out of range, Python returns a empty sequence:
>>
>> py> L = [2, 4, 8, 16]
>> py> L[5] # out of range, raises IndexError
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> IndexError: list index out of range
>> py> L[5:6] # out of range slice return empty list
>> []
>>
>> To get a default:
>>
>> py> L[5:6] or -1
>> -1
>>
>>
>> This is short and simple enough to use in place, but we can also wrap this
>> into a convenient helper function:
>>
>> def get(seq, index, default=None):
>> return (seq[index:index+1] or [default])[0]
>>
>>
>>
>> py> get(L, 2, -1)
>> 8
>> py> get(L, 200, -1)
>> -1
>
> But note:
>
>>>> def get(seq, index, default=None):
> ... return (seq[index:index+1] or [default])[0]
> ...
>>>> get("abc", -1, "default")
> 'default'
>
> God old try...except to the rescue:
>
>>>> def get(seq, index, default=None):
> ... try: return seq[index]
> ... except IndexError: return default
Replace IndexError with (IndexError, KeyError) and the functions works
with any subscriptable that raises either exception.
> ...
>>>> get("abc", -1, "default")
> 'c'
>
>
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Slice equivalent to dict.get Steven D'Aprano <steve@pearwood.info> - 2016-04-01 02:07 +1100 Re: Slice equivalent to dict.get Peter Otten <__peter__@web.de> - 2016-03-31 17:24 +0200 Re: Slice equivalent to dict.get Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-31 09:43 -0600 Re: Slice equivalent to dict.get "Sven R. Kunze" <srkunze@mail.de> - 2016-03-31 18:05 +0200 Re: Slice equivalent to dict.get Terry Reedy <tjreedy@udel.edu> - 2016-03-31 13:51 -0400 Re: Slice equivalent to dict.get Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-03-31 14:28 -0500
csiph-web