Inserting a string into a list without getting split into characters

后端 未结 9 1733
孤城傲影
孤城傲影 2020-12-13 16:45

I\'m new to Python and can\'t find a way to insert a string into a list without it getting split into individual characters:

>>> list=[\'hello\',\'w         


        
相关标签:
9条回答
  • 2020-12-13 17:26

    Another option is using the overloaded + operator:

    >>> l = ['hello','world']
    >>> l = ['foo'] + l
    >>> l
    ['foo', 'hello', 'world']
    
    0 讨论(0)
  • 2020-12-13 17:27

    I suggest to add the '+' operator as follows:

    list = list + ['foo']

    Hope it helps!

    0 讨论(0)
  • 2020-12-13 17:28

    Don't use list as a variable name. It's a built in that you are masking.

    To insert, use the insert function of lists.

    l = ['hello','world']
    l.insert(0, 'foo')
    print l
    ['foo', 'hello', 'world']
    
    0 讨论(0)
提交回复
热议问题