Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'value,': 0.04; 'element': 0.07; 'modified': 0.07; 'string': 0.09; 'iterate': 0.09; 'rows': 0.09; 'sql,': 0.09; 'def': 0.12; '\'"\'': 0.16; 'columns': 0.16; 'simplest': 0.16; 'tuple': 0.16; 'index': 0.16; '<': 0.19; 'this:': 0.26; 'values': 0.27; 'compared': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'concise': 0.31; 'tuples': 0.31; 'skip:m 30': 0.32; 'received:209.85': 0.35; 'convert': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'i.e.': 0.36; 'should': 0.36; 'list': 0.37; 'received:209': 0.37; 'checks': 0.38; 'skip:m 40': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'list,': 0.38; 'quote': 0.39; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'new': 0.61; 'charset:windows-1252': 0.65; 'containing': 0.69; 'results,': 0.84; 'skip:\x91 10': 0.84; '8bit%:33': 0.91 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-type; bh=P4TMl4/S25qE/+twl6T9PChZgJlZI8ptbtkJjmTddQY=; b=byjdL9/t3HGRHGuDY2IzsQqJpPbaJJc2wEdDvXU4q91AxDnZj/g8UqoWueGq7kv9FB uSY/JGJVaOAavndZC77ZbwtXBnx4RJkDPp9GhOEBAvNTjmEtEfdpl3khLQr5eryWBlGz ff6Pw+8V8/sXBJToXYS+FCVBIAyGSwVrIgI0sr6yd9DzQZk5t5WsZxXwTW9H/SybmI3e yM9Z8ZfChydN0cM6Cb8QK+LWhA2BVC+z4fKOzhHXwNyzJRm0V5ESHtgmmfBzMThcH3zV mhPTGdNV3gaB4OBhr2GoEQGs7Y2aWe7yCzeiuZVhp4cfrYr2+w+xA98EC1tCcGKV1UAH 79Jw== X-Gm-Message-State: ALoCoQkwzmzgBFQ5wNQkXBd/wcYm5o2I0DG3TZGqSB0fQGyKkFbZhJoLQwsmBj4D3Kb2iiq1da3E MIME-Version: 1.0 X-Received: by 10.43.10.198 with SMTP id pb6mr441847icb.40.1379712732522; Fri, 20 Sep 2013 14:32:12 -0700 (PDT) Date: Fri, 20 Sep 2013 17:32:12 -0400 Subject: Iterate through a list of tuples for processing From: Shyam Parimal Katti To: python-list@python.org Content-Type: multipart/alternative; boundary=bcaec50e5ce7507c1604e6d76707 X-Mailman-Approved-At: Fri, 20 Sep 2013 23:54:45 +0200 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: 197 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379714085 news.xs4all.nl 15905 [2001:888:2000:d::a6]:37665 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54512 --bcaec50e5ce7507c1604e6d76707 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable I have a list of tuples where the number of rows in the list and the number of columns in tuples of the list will not be constant. i.e. list =3D [(a1,b1, =85z1), (a2,b2, =85, z2),=85. ,(am,bm, =85 , zm )]. It ca= n be compared to the SQL results, as the number of columns change in the sql, the number of columns change in the list of tuples as well. I have to iterate through each element of each tuple in the list, perform some checks on the value, convert it and return the modified values as a new list of tuples. 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)] The simplest approach for me would be to do this: mod_val =3D [transform(row) for row in list_value] def transform(row): mod_list=3D[] while index < len(row): ... if isinstance(row[index],basestring): ... if '"' in row[index]: ... mod_list.append('"'+row[index]+'"') ... else: ... mod_list.append(row[index]) ... index =3D index+1 ... return mod_list Is there a way to make the code concise using list comprehension? --bcaec50e5ce7507c1604e6d76707 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable

I have a list of tuples where the number of rows in the list and the number of colum= ns in tuples of the list will not be constant. i.e.

= =A0

l= ist =3D [(a1,b1, =85z1), (a2,b2, =85, z2),=85. ,(am,bm, =85 , zm )]. It can be compared to t= he SQL results, as the number of columns change in the sql, the number of columns change in the list of tuples as well.

= =A0

I= have to iterate through each element of each tuple in the list, perform some che= cks on the value, convert it and return the modified values as a new list of tu= ples.

= =A0

i= .e.

l= ist_value =3D [(=91name1=92, 1234, =91address1=92 ), (=91name2=92, 5678, =91address2= =92), (=91name3=92, 1011, =91addre=94ss3=92)]

= =A0

I= need to access each value to check if the value contains a double quote and encl= ose the string containing double quote with double quotes. The transformation should return

= =A0

l= ist_value =3D [(=91name1=92, 1234, =91address1=92 ), (=91name2=92, 5678, =91address2= =92), (=91name3=92, 1011, =91=94addre=94ss3=94=92)]

= =A0

T= he simplest approach for me would be to do this:

= =A0

m= od_val =3D [transform(row) for row in list_value]

= =A0

d= ef transform(row):

= =A0=A0 mod_list=3D[]

= =A0=A0 while index < len(row):

.= ..=A0=A0=A0 if isinstance(row[index],basestring):

.= ..=A0=A0=A0=A0=A0=A0 if '"' in row[index]:

.= ..=A0=A0=A0=A0=A0=A0=A0=A0=A0 mod_list.append('"'+row[index]+'"')

.= ..=A0=A0=A0 else:

.= ..=A0=A0=A0=A0=A0=A0 mod_list.append(row[index])

.= ..=A0=A0=A0 index =3D index+1

.= .. return mod_list

= =A0

= =A0

I= s there a way to make the code concise using list comprehension?=A0

--bcaec50e5ce7507c1604e6d76707--