Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'subject:error': 0.03; 'string': 0.09; 'immutable': 0.09; 'type,': 0.09; 'python': 0.11; '75))': 0.16; 'length:': 0.16; 'one-element': 0.16; 'range(0,': 0.16; 'string).': 0.16; 'thread,': 0.16; 'index': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'tests': 0.22; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'character': 0.29; 'generally': 0.29; 'message-id:@mail.gmail.com': 0.30; 'membership': 0.31; '25,': 0.31; '50,': 0.31; 'exceptions': 0.31; 'larry': 0.31; 'skip:7 10': 0.31; 'comment': 0.34; 'objects': 0.35; 'operations': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'sequence': 0.36; 'operating': 0.37; 'list': 0.37; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'most': 0.60; 'name:': 0.61; 'range': 0.61; 'such': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'object:': 0.84; 'observed': 0.84; 'subject:Lists': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=8YVTN5KPgZe82sX1o1jpGLlUI55BzK80GmyQ58kRtzk=; b=kqKqBy6VRJf9mB0qVygRDbOtea+LCjFHTfJoo0cqh1ymwNRz4qr1PvtVfQAUoSm0ly e4T6oYRp2Rpgs8s2w6yfKVbE/EMF8k2mAdHcSceG8Di4pZOoyAE8z90QakxvalMjtpRg TJBVEjH91Yt1sO4G8nN3/m2/TcCQFJv4CBihN7bWZCmb6J03HNvZa6MyYG2PV7AJb2Xm 5dfeAEq9ZzxQQdH9BnzmN7QGCllRmFRQ115tz/hqYQKegU51t4O4Z9tzIlR4f4hBrEVW oq8aTWCUP2W22sLf7DGHWry9E4JgMLO0tQlZjMi3R1XcOGAWYKDM5QDXyWLYE+d/oh53 XeUg== X-Received: by 10.66.191.135 with SMTP id gy7mr11484281pac.95.1414246666809; Sat, 25 Oct 2014 07:17:46 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <3IudnRDHO76s2tbJnZ2dnUU7-VednZ2d@giganews.com> References: <3082ee74-3182-4ea5-9851-2b1326e5b34c@googlegroups.com> <9a1l4athfj10h7r3v69ddo95285n9cjlms@4ax.com> <3IudnRDHO76s2tbJnZ2dnUU7-VednZ2d@giganews.com> From: Ian Kelly Date: Sat, 25 Oct 2014 08:17:06 -0600 Subject: Re: I am out of trial and error again Lists To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414246675 news.xs4all.nl 2855 [2001:888:2000:d::a6]:54821 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79670 On Sat, Oct 25, 2014 at 12:46 AM, Larry Hudson wrote: >> name="123-xyz-abc" >> for x in name: >> if x in range(10): > > x is a character (a one-element string). range(10) is a list of ints. A > string will never match an int. BTW, as it is used here, range(10) is for > Py2, for Py3 it needs to be list(range(10)). The last comment is incorrect. You can do membership tests on a Python 3 range object: >>> range(2, 4) range(2, 4) >>> for i in range(5): ... print(i, i in range(2, 4)) ... 0 False 1 False 2 True 3 True 4 False You can also index them: >>> range(50, 100)[37] 87 slice them: >>> range(0, 100, 2)[:25] range(0, 50, 2) get their length: >>> len(range(25, 75)) 50 search them: >>> range(25, 75).index(45) 20 and generally do most operations that you would expect to be able to do with an immutable sequence type, with the exceptions of concatenation and multiplication. And as I observed elsewhere in the thread, such operations are often more efficient on range objects than by constructing and operating on the equivalent lists.