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
3

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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,}

2 Comments

sorry bro that needs a lot of clarification, but actually I tested that was wrong so sorry!
idea is good but I would use for i, item in enumerate(array): instead of range(len(array))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.