Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!weretis.net!feeder1.news.weretis.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'nested': 0.07; 'arguments': 0.09; 'idea?': 0.09; 'iterate': 0.09; 'cc:addr :python-list': 0.11; '*args': 0.16; 'arent': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'loops': 0.16; 'subject:skip:m 10': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'wed,': 0.18; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'thus': 0.29; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'code': 0.31; 'bunch': 0.31; 'cap': 0.31; 'dimensions': 0.31; 'supposed': 0.32; 'run': 0.32; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'pm,': 0.38; 'sure': 0.39; 'how': 0.40; 'worry': 0.60; 'making': 0.63; 'faster.': 0.84; 'subject:space': 0.84; 'worry,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=GlrVtB6R2chfAZekGYo/fO70MKAJfN/2wugTFuSvqi4=; b=PF6MLX/UcIaKYFyQSOzeVVzGb5CQ4piY+z4+ZqXU9h5CqburqAt08GeBzE87PhFNDK UGvOxkSnMNnepGVeGR5i+wDl9lmtWnV/ikdvFTJoyo8t37IfWnG0AnskMME2L4YulWAL JL5Z7wI/X2Fqxlxw2jIWVHFo8NKWlQtYAhWhslltd0vCcRUDAWTTTDsKC25XSZk6N4rg iGvtmtp/dKur2nKm+bufHTcwMGk4s6HEj9XN5gzNmj40oyic5duLTN4yYYdLOc1lyFuz DisNRC7/X+6+gS2cy8914dA0Cs2UfzAy5G8XrN5tIw58Y9AdpdaRWp9Pb2l4gwYzpltt xliQ== MIME-Version: 1.0 X-Received: by 10.42.216.148 with SMTP id hi20mr11709930icb.12.1407306344120; Tue, 05 Aug 2014 23:25:44 -0700 (PDT) In-Reply-To: References: Date: Wed, 6 Aug 2014 16:25:44 +1000 Subject: Re: Pythonic way to iterate through multidimensional space? From: Chris Angelico Cc: "python-list@python.org" 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: 16 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407306352 news.xs4all.nl 2914 [2001:888:2000:d::a6]:56454 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75781 On Wed, Aug 6, 2014 at 3:34 PM, Gayathri J wrote: > Below is the code I tried to check if itertools.product() was faster than > normal nested loops... > > they arent! arent they supposed to be...or am i making a mistake? any idea? Don't worry about what's faster. That almost never matters. Worry, instead, about how you would code it if you can't be sure how many dimensions there'll be until run time (which the OP said can happen). With product(), you can give it a variable number of arguments (eg with *args notation), but with loops, you'd need to compile up some code with that many nested loops - or at best, something where you cap the number of loops and thus dimensions, and have a bunch of them iterate over a single iterable. ChrisA