print() a list

Mark Tolonen metolone+gmane at gmail.com
Sat Sep 5 08:35:35 CEST 2009


"DarkBlue" <pict100 at gmail.com> wrote in message 
news:b9c0c4ac-5f8f-4133-b928-9e55ab4b22a0 at x5g2000prf.googlegroups.com...
>I am trying to get used to the new print() syntax prior to installing
> python 3.1:
>
> test=[["VG", "Virgin Islands, British"],["VI", "Virgin Islands, U.S."],
> ["WF", "Wallis and Futuna"],["EH", "Western Sahara"],["YE", "Yemen"],
> ["ZM", "Zambia"],["ZW", "Zimbabwe"],]
>
> #old print
>
> for z in test:
>  if z[0].startswith('W'):
>     print z[0] ,  z[1]
>
> print
>
>
> # new print()
> # now a list would have to be printed like this to be equal to old
> print ?
>
> for z in test:
>  if z[0].startswith('W'):
>     print('%s %s') % (z[0] ,  z[1])
>
> print
>
> # this output prints the brackets etc. too, not what we want
>
> for z in test:
> if z[0].startswith('W'):
>    print(z[0] , z[1])
>
> print
>
>
>
> on python 2.6 I get following output:
>
> WF Wallis and Futuna
>
> WF Wallis and Futuna
>
> ('WF', 'Wallis and Futuna')
>
>
> Before actually installing python 3.1 my question is if the py2to3
> converter also considers this situation ?

Without the following statement, print does not work the "new" way.  What 
you are printing is a tuple of the two list elements.

from __future__ import print_function

test = [
    ["VG", "Virgin Islands, British"],
    ["VI", "Virgin Islands, U.S."],
    ["WF", "Wallis and Futuna"],
    ["EH", "Western Sahara"],
    ["YE", "Yemen"],
    ["ZM", "Zambia"],
    ["ZW", "Zimbabwe"]]

for z in test:
  if z[0].startswith('Z'):
     print(z[0],z[1])
print()

----results----
ZM Zambia
ZW Zimbabwe

Comment out "from __future__ import print_function" and you'll get:





More information about the Python-list mailing list