Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Slice equivalent to dict.get Date: Thu, 31 Mar 2016 17:24:50 +0200 Organization: None Lines: 59 Message-ID: References: <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 55Z8lUmXfnM+tqlKsvV9CAq/Z/CNzGHLh6iNyHEYTL3w== 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; '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; '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: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; 'sequence:': 0.16; 'slice.': 0.16; 'wrote:': 0.16; 'helper': 0.18; 'try:': 0.18; '>>>': 0.20; '"",': 0.22; 'function:': 0.22; 'tuples': 0.22; '(most': 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; "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; 'received:de': 0.40; 'default': 0.61; 'note:': 0.66; 'god': 0.67 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd993c.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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:106167 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 ... >>> get("abc", -1, "default") 'c'