-2

What is the best way in python to convert a list of small length (3 or 4) into variables. Actually I want to do the opposite of creating a list like this in Python 3.

some_python_list=[a,b,c,d]

So what I want is something which does

a,b,c,d=some_python_list.decompose(4) #while I know alist is of length 4

So I can save myself from

a=some_python_list[0]
b=some_python_list[1]
c=some_python_list[2]
d=some_python_list[3]

or

a,b,c,d=some_python_list[0],some_python_list[1],some_python_list[2],some_python_list[3]
Ahsan Roy
  • 201
  • 1
  • 2
  • 15

1 Answers1

3

If you know the length of the list, then you can use a number of variables equal to the number of elements in the list:

some_python_list = ['1', '2', '3', '4']
a, b, c, d = some_python_list
print(a, b, c, d)

Output:

1 2 3 4
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55