Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Slice equivalent to dict.get Date: Thu, 31 Mar 2016 13:51:24 -0400 Lines: 69 Message-ID: References: <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com> 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: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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:106177 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 "", line 1, in >> 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