Prepend prefix to list elements with list comprehension

前端 未结 2 1712
花落未央
花落未央 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 04:52

    With list comprehensions, you're creating new lists, not appending elements to an existing list (which may be relevant on really large datasets)

    Why does it have to be a list comprehension anyway? Just because python has them doesn't make it bad coding practice to use a for-loop.

提交回复
热议问题