0

Hey guys! How do you create variables through iteration

I want to assign every item in a list to a variable, but the length of the list is dynamic. There is no specific limit to the amount of items the user can input

5th Apr 2020, 3:45 PM
Suleiman Sani
Suleiman Sani - avatar
3 odpowiedzi
+ 4
for i in range(5): locals()["a"+str(i)]=i print(a1) l = [1,2,3] for i in l: locals()["a"+str(i)]=i print(a1)
5th Apr 2020, 4:11 PM
Oma Falk
Oma Falk - avatar
+ 2
but it has a variable... l=[1, 2,3] print(l[0])# first item is l[0]
5th Apr 2020, 4:15 PM
Oma Falk
Oma Falk - avatar
+ 1
Thanks guys! The first answer works well with numbers but when strings are in the list, it brings out error but the second answer seems to handle both well. Thanks again
6th Apr 2020, 12:30 AM
Suleiman Sani
Suleiman Sani - avatar