Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!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.058 X-Spam-Evidence: '*H*': 0.88; '*S*': 0.00; 'skip:[ 20': 0.04; 'string': 0.09; 'rows': 0.09; '\'"\'': 0.16; 'columns': 0.16; 'elem': 0.16; 'retained': 0.16; 'do,': 0.16; '>>>': 0.22; 'header :In-Reply-To:1': 0.27; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'concise': 0.31; 'tuples': 0.31; 'url:python': 0.33; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'i.e.': 0.36; 'url:listinfo': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'changing': 0.37; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'quote': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'charset:windows-1252': 0.65; 'containing': 0.69; 'skip:\x91 10': 0.84; 'suits': 0.84; '8bit%:33': 0.91 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:to :content-type:content-transfer-encoding; bh=DdQzQcTlVW03YeP/083tL1m+IA2yRHPKPY6FZh7jo2w=; b=TLZCM5w+yJYayO4TPKEczI2wZfjPY79sFX68dBngeyd8TC4TRaH7lRodBHD7paoFPM f/ImZzdfCzztcyH4B9WJVNWRe5TI3eNlRhVtrz2821WqYHaS90jlQC/Zh4i/NxluGBcf F9c0IPYJhhW+WcAeDbs5ZrLDZZO4Fs0FwwitwK8WF8s69LiiWM00NT0ALYJmKpyulmTV fm/KEq9x+gdOqMuNGaDpYH/nYwuT2WiwUQ3+KFUXR/53HcMwoor9DW34NjDWnC80/bGY aElzF7cO+rBLjV1j56rCZjQoFBuZS/Zup4Tmro3NSRBLMKWVwSOOxrMgggKVsFKnRE4t KBFg== MIME-Version: 1.0 X-Received: by 10.182.22.226 with SMTP id h2mr8071490obf.8.1379719514014; Fri, 20 Sep 2013 16:25:14 -0700 (PDT) In-Reply-To: References: Date: Sat, 21 Sep 2013 01:25:13 +0200 Subject: Re: Iterate through a list of tuples for processing From: Vlastimil Brom To: python Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379719522 news.xs4all.nl 15981 [2001:888:2000:d::a6]:44195 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54522 2013/9/20 Shyam Parimal Katti : > I have a list of tuples where the number of rows in the list and the numb= er > of columns in tuples of the list will not be constant. i.e. > > ...> i.e. > > list_value =3D [(=91name1=92, 1234, =91address1=92 ), (=91name2=92, 5678,= =91address2=92), > (=91name3=92, 1011, =91addre=94ss3=92)] > > I need to access each value to check if the value contains a double quote > and enclose the string containing double quote with double quotes. The > transformation should return > > list_value =3D [(=91name1=92, 1234, =91address1=92 ), (=91name2=92, 5678,= =91address2=92), > (=91name3=92, 1011, =91=94addre=94ss3=94=92)] > > ...> > > > Is there a way to make the code concise using list comprehension? > > > -- > https://mail.python.org/mailman/listinfo/python-list > Hi, would the following do, what you want? >>> orig_list =3D [('name1', 1234, 'address1' ), ('name2', 5678, 'address2'= ), ('name3', 1011, 'addre"ss3')] >>> modif_list =3D [['"'+elem+'"' if isinstance(elem, basestring) and '"' i= n elem else elem for elem in row] for row in orig_list] >>> modif_list [['name1', 1234, 'address1'], ['name2', 5678, 'address2'], ['name3', 1011, '"addre"ss3"']] >>> I guess, you don't mind changing the inner tuples to lists, but the original structure could be retained as well: >>> [tuple(['"'+elem+'"' if isinstance(elem, basestring) and '"' in elem el= se elem for elem in row]) for row in orig_list] [('name1', 1234, 'address1'), ('name2', 5678, 'address2'), ('name3', 1011, '"addre"ss3"')] >>> Of course, you have to decide, whether the readability/conciseness suits your needs ... hth, vbr