1','
  • 2'] How can I get goalout l? I play with ...">

    Does Python "join" on either side of the lines?

    l = ['1','2','3']
    goal = ['<li>1</li>','<li>2</li>']
    

    How can I get goalout l?

    I play with a list, but it's messy!

    +5
    source share
    3 answers

    Try string formatting and list comprehension, for example.

    goal = ['<li>{0}</li>'.format(x) for x in l]
    
    +10
    source

    Two options using str.format():

    goal = map('<li>{0}</li>'.format, l)
    

    ... or...

    goal = ['<li>{0}</li>'.format(x) for x in l]
    

    Note that in Python 3.x, an map()iterator will be returned instead of a list, so if you need a list, you will need to use list(map(...)).

    +1
    source

    Using method string.format

    goal = ['<li>{0}</li>'.format(sym)  for sym in l]
    
    0
    source

    All Articles