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


Groups > comp.lang.python > #106171

Re: Slice equivalent to dict.get

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Sven R. Kunze" <srkunze@mail.de>
Newsgroups comp.lang.python
Subject Re: Slice equivalent to dict.get
Date Thu, 31 Mar 2016 18:05:44 +0200
Lines 20
Message-ID <mailman.268.1459440355.28225.python-list@python.org> (permalink)
References <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de dEMpOdz5c0lo0lbCNdBmCgAcpxiMaDtIAF/pEVt8kQgA==
Return-Path <srkunze@mail.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.029
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; '[1,': 0.09; 'python': 0.10; "'a',": 0.16; 'range,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequence:': 0.16; 'slice.': 0.16; 'wrote:': 0.16; 'tuples': 0.22; 'trying': 0.22; 'header:In-Reply- To:1': 0.24; 'equivalent': 0.27; "d'aprano": 0.33; 'steven': 0.33; 'this?': 0.34; 'received:10.0': 0.34; 'lists': 0.34; 'best,': 0.35; "isn't": 0.35; 'sometimes': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'method': 0.37; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'default': 0.61; 'here.': 0.62; 'charset:windows-1252': 0.62
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1459440353; bh=7Fix6p7J3RnDq257gDPE5bU0t1bHBrd2QUDqLPptO6s=; h=Subject:To:References:From:Date:In-Reply-To:From; b=yEHOCGXQV2MEQhyMk86Y5IFKU47GBWUpI0qN0D6IBPcP2esMLPZfhd+Y1U5J1TqSH 26iPAV5jklo1GXdWG3Gaad8UbkSlL0OLBJ77NhLY1/JeVui56Jo7Ga1BiEQlcZAmGq Mf9jg0/2TvUZ1o9s74paPzVjkr6kRPKW2rkmP3IY=
In-Reply-To <56fd3d17$0$1606$c3e8da3$5496439d@news.astraweb.com>
X-purgate clean
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate-type clean
X-purgate-Ad Categorized by eleven eXpurgate (R) http://www.eleven.de
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate clean
X-purgate-size 509
X-purgate-ID 154282::1459440353-000018ED-AC2F3FF9/0/0
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:106171

Show key headers only | 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