Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Sven R. Kunze" Newsgroups: comp.lang.python Subject: Re: Slice equivalent to dict.get Date: Thu, 31 Mar 2016 18:05:44 +0200 Lines: 20 Message-ID: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:106171 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