Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106167
| 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 | <mailman.265.1459437901.28225.python-list@python.org> (permalink) |
| 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 | <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; '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 <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:106167 |
Show key headers only | View raw
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
...
>>> get("abc", -1, "default")
'c'
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