Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106170
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Slice equivalent to dict.get |
| Date | 2016-03-31 09:43 -0600 |
| Message-ID | <mailman.267.1459439072.28225.python-list@python.org> (permalink) |
| References | <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com> <ndjfg3$b3k$1@ger.gmane.org> |
On Thu, Mar 31, 2016 at 9:24 AM, Peter Otten <__peter__@web.de> wrote:
> But note:
>
>>>> def get(seq, index, default=None):
> ... return (seq[index:index+1] or [default])[0]
> ...
>>>> get("abc", -1, "default")
> 'default'
The discontinuity between -1 and 0 in indexing is a pain in the rear.
Negative indexes are one of those things that may seem like a good
idea at first, but I think in the end it causes more trouble than it's
worth. There's nothing you can do with L[-x] that you can't do more
explicitly with L[len(L) - x].
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