variable length print format

Nizar Batada nbatada at stanford.edu
Tue May 7 15:19:23 EDT 2002


string.join(map(lambda x: "%s: %s" % (x[0],x[1]), zip(range(1,len(List)), List)))

On 7 May 2002, les ander wrote:

> Hi,
> i have a variable length list of numbers.
> i would like to print them out with indicies in front as shown below:
>
> List=['a','b','c','d'] ---> 1:a 2:b 3:c 4:d
>
> one obvious solution is that i do
> for i in range(List):
>   print "%s:%s" % (i+1,List[i])
>
> however i have a lot of these and would like it to be faster so i
> tried doing this
>
> formatstr=" %s:%s " * len(List)
>
> print formatstr % (range(1,len(List)+1), List)
>
> but it did not work.
>
> is there a way to do this another way?
> les
>


-------------------------------------------------------------------
N i z a r   B a t a d a		 	  nbatada at stanford.edu
-------------------------------------------------------------------
Never let your schooling interfere with your education--Mark Twain
--------------------------------------------------------------------




More information about the Python-list mailing list