Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'output': 0.05; 'args': 0.07; 'subject:form': 0.07; 'string': 0.09; 'fixed,': 0.09; 'imply': 0.09; 'indexes': 0.09; 'interpreted': 0.09; 'lawrence': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'strings.': 0.09; 'subject:number': 0.09; 'subject:string': 0.09; 'works.': 0.09; 'jan': 0.12; 'concatenated': 0.16; 'integers.': 0.16; 'iterable': 0.16; 'length.': 0.16; 'loops': 0.16; 'ravi': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'remainder': 0.16; 'subject: \n ': 0.16; 'language': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'sort': 0.25; "i've": 0.25; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'ignored.': 0.30; 'needed.': 0.30; 'subject:list': 0.30; "i'm": 0.30; 'faster,': 0.31; 'keys': 0.31; 'mini': 0.31; 'anyone': 0.31; 'class': 0.32; 'third': 0.33; 'could': 0.34; 'definition': 0.35; 'but': 0.35; 'sequence': 0.36; 'words,': 0.36; 'hi,': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'list': 0.37; 'expected': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'length': 0.61; 'new': 0.61; 'received:173': 0.61; 'simple': 0.61; 'first': 0.61; 'field': 0.63; 'fourth': 0.84; 'received:fios.verizon.net': 0.84; 'subject:needed.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Concatenate string list to number list to form title - Logic needed. Date: Mon, 16 Dec 2013 15:23:14 -0500 References: <2333bfb4-cd72-4ed0-9b28-d8dbe26b5be2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-254-207.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387225409 news.xs4all.nl 2976 [2001:888:2000:d::a6]:49809 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62099 On 12/16/2013 12:43 PM, Mark Lawrence wrote: > On 16/12/2013 17:16, Ravi Prabakaran wrote: >> Hi, >> I'm completely new to python. I just need simple logic to get output >> without any loops. >> I have list of string The remainder of your description and example imply that this must be a sequence of exactly two strings. In other words, 'list' is both too specific as to class and not specific enough as to length. >> and list of list of numbers. While this must be an iterable of sequences of exactly 4 numbers. >> Each string should be concatenated with every third and fourth values >> to generate proper titles in list of strings. >> >> t = ['Start','End'] >> a = [[1,2,3,4], >> [5,6,7,8]] >> >> Expected Result : ( list of strings ) >> >> ['Start - 3 , End - 4', >> 'Start - 7 , End - 8'] >> >> Note : First 2 values from each list should be ignored. >> >> Could anyone please guide me with best solution without loops ? If the length of the iterable of sequences is not fixed, a loop of some sort is needed. > I've no idea what your definition of "best" is but this works. > > strings = ['{} - {} , {} - {}'.format(t[0], b[-2], t[1], b[-1]) for b in a] Very nice. The following use the format mini language to do the indexing, avoiding passing the args twice each. strings = ['{0[0]} - {1[2]} , {0[1]} - {1[3]}'.format(t, b) for b in a] form = '{src[0]} - {val[2]} , {src[1]} - {val[3]}' strings = [form.format(src=t, val=b) for b in a] The first should be faster, but may be less readable. Note that '-2' and '-1' do not work as int indexes in the field names because they would be interpreted as string keys rather than integers. -- Terry Jan Reedy