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


Groups > comp.lang.python > #106171

Re: Slice equivalent to dict.get

From "Sven R. Kunze" <srkunze@mail.de>
Newsgroups comp.lang.python
Subject Re: Slice equivalent to dict.get
Date 2016-03-31 18:05 +0200
Message-ID <mailman.268.1459440355.28225.python-list@python.org> (permalink)
References <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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