variable length print format

John La Rooy larooy at xtar.co.nz
Thu May 9 04:40:06 EDT 2002


Here's a couple of one-liners to try

>>> List=['a','b','c','d']
>>> print " ".join([ "%d:%s"%(x+1,List[x]) for x in range(len(List)) ])
1:a 2:b 3:c 4:d
>>> print " ".join([ "%d:%s"%(x+1,y) for x,y in zip(range(len(List)),List) ])
1:a 2:b 3:c 4:d




On 7 May 2002 19:38:52 -0700
bbeck13 at excite.com (Brandon Beck) wrote:

> Hi Les,
> 
> Give this a shot:
> 
>      lst = ['a', 'b', 'c', 'd']
>      tuples = [ (i,elem) for (i, elem) in zip(range(1, 1+len(lst)), lst) ]
>      s = string.join(map(lambda t: "%i:%s"%t, tuples))
>      print s
> 
> 
> Brandon
> 
> 
> 
> les_ander at yahoo.com (les ander) wrote in message news:<a2972632.0205071050.9e68201 at posting.google.com>...
> > 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
> > 



More information about the Python-list mailing list