It is necessary to unpack the line in the list. How to do it right?

mylist = [] stroka = "abcdef" *mylist = stroka 

like this

  • one
    mylist = list(stroka) ? - MaxU
  • mylist.append ("abcdef") @Oleg may be this option, just do not unpack and add to - DancingMaster
  • Yes, it is clear. I wanted to unpack through * see. For example: first, * mylist = stroka will be everything except the first character, so it was thought, maybe like brackets, you can fold out the entire line into the array. - Oleg

2 answers 2

Actually, if you carefully read the error message when using the expression

 >>> s = "string" >>> *l = s *l = s ^ SyntaxError: starred assignment target must be in a list or tuple 

it can be understood that it is enough to have a left tuple or list:

 >>> *l, = s >>> l ['s', 't', 'r', 'i', 'n', 'g'] >>> [*l] = s >>> l ['s', 't', 'r', 'i', 'n', 'g'] 

Since in Python in many cases you can create a tuple using only a comma, *l, = s simply a short form of the expression (*l, ) = s

  • Thank. This is exactly what was asked. Did not guess the comma after add :) - Oleg

By the way, in python you can work with strings as with an array:

  In [2]: s = "Hello, world!"

 In [3]: s [1] # Already almost an array!
 Out [3]: 'e'

 In [4]: ​​s [1: 4]
 Out [4]: ​​'ell'

 In [5]: mylist = list (s) # Here's what you need
 In [6]: mylist
 Out [6]: ['H', 'e', ​​'l', 'l', 'o', ',', '', 'w', 'o', 'r', 'l', ' d ','! ']

 In [8]: type (mylist)
 Out [8]: builtins.list # Array!

 In [9]: type (s)
 Out [9]: builtins.str # String!
 
  • The question was precisely in the use of the unpacking method. For mylist = list (s) I am aware. - Oleg