Prepend prefix to list elements with list comprehension

前端 未结 2 1714
花落未央
花落未央 2021-02-13 04:03

Having a list like this:

[\'foo\',\'spam\',\'bar\']

is it possible, using list comprehension, to obtain this list as result?

[\         


        
2条回答
  •  别那么骄傲
    2021-02-13 05:03

    In [67]: alist = ['foo','spam', 'bar']
    
    In [70]: [prefix+elt for elt in alist for prefix in ('','ok.') ]
    Out[70]: ['foo', 'ok.foo', 'spam', 'ok.spam', 'bar', 'ok.bar']
    

提交回复
热议问题