Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106166 > unrolled thread

Slice equivalent to dict.get

Started bySteven D'Aprano <steve@pearwood.info>
First post2016-04-01 02:07 +1100
Last post2016-03-31 14:28 -0500
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  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

#106166 — Slice equivalent to dict.get

FromSteven D'Aprano <steve@pearwood.info>
Date2016-04-01 02:07 +1100
SubjectSlice equivalent to dict.get
Message-ID<56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com>
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



-- 
Steven

[toc] | [next] | [standalone]


#106167

FromPeter Otten <__peter__@web.de>
Date2016-03-31 17:24 +0200
Message-ID<mailman.265.1459437901.28225.python-list@python.org>
In reply to#106166
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'

[toc] | [prev] | [next] | [standalone]


#106170

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-03-31 09:43 -0600
Message-ID<mailman.267.1459439072.28225.python-list@python.org>
In reply to#106166
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].

[toc] | [prev] | [next] | [standalone]


#106171

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-03-31 18:05 +0200
Message-ID<mailman.268.1459440355.28225.python-list@python.org>
In reply to#106166
On 31.03.2016 17:07, 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:

I see what you are trying to achieve here. What do you think about this?

[1, 2, 3].get(999, '?')


Best,
Sven

[toc] | [prev] | [next] | [standalone]


#106177

FromTerry Reedy <tjreedy@udel.edu>
Date2016-03-31 13:51 -0400
Message-ID<mailman.274.1459446705.28225.python-list@python.org>
In reply to#106166
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

[toc] | [prev] | [next] | [standalone]


#106180

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2016-03-31 14:28 -0500
Message-ID<mailman.277.1459452550.28225.python-list@python.org>
In reply to#106166
On Thu, Mar 31, 2016 at 12:51 PM, Terry Reedy <tjreedy@udel.edu> wrote:
> On 3/31/2016 11:24 AM, Peter Otten wrote:
>> 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.

Or use the superclass of both of those, LookupError, and also catch
anything that uses that more generic name as well.

-- 
Zach

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web