0

The array size is vary depending on the user input

for example with size of 3

array = [1,3,7]

to get

a = 1
b = 3
c = 7
MR_tadoo
  • 9
  • 5
babo
  • 11
  • 3
  • 3
    Does this answer your question? [How to assign each element of a list to a separate variable?](https://stackoverflow.com/questions/19300174/how-to-assign-each-element-of-a-list-to-a-separate-variable) – Nick is tired Jun 24 '20 at 02:36
  • Preferably use [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) though – Nick is tired Jun 24 '20 at 02:38
  • don't waste time for this. Keep it as array and use `array[0]`, `array[1]` or even `array[other_variable_with_number]`. And `array` will be more useful when you have to iterate it - `for item in array: ...` or check how many elements you have `len(array)`, etc. – furas Jun 24 '20 at 02:52

2 Answers2

1

Use enumerate to get both the number and the index at the same time.

array = [1,3,7]
for pos, num in enumerate(array):
    print(pos,num)

This will give you a output like this:

0 1
1 3
2 7

So now to access the number at index 0, simply write,

for pos, num in enumerate(array):
    if pos == 0:
        print(num)

Which will give you this output:

1

Hope this helps.

Farhan Bin Amin
  • 38
  • 3
  • 11
1

if you have name of the variables than you can use this

(a,b,c) = array

You probably want a dict instead of separate variables. For example

variavle_dictionary = {}
for i in range(len(array)):
    variavle_dictionary["key%s" %i] = array[i]

print(variavle_dictionary)

{'key0': 1, 'key1': 2, 'key2': 3,}
Dharm paaji
  • 46
  • 10