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
3 Antworten
+ 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)
+ 2
but it has a variable...
l=[1, 2,3]
print(l[0])# first item is l[0]
+ 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