so I have a loop, and I want to call a variable for example name and add x from a loop to the end of the variable's name, so it looks like name0.
for example:
for x, var enumurate(something):
name + x = something
it gives me out error.
so I have a loop, and I want to call a variable for example name and add x from a loop to the end of the variable's name, so it looks like name0.
for example:
for x, var enumurate(something):
name + x = something
it gives me out error.
What you ask is possible, but should not be done.
>>> foo = ['fee', 'fie', 'foe', 'fum']
>>> for i, val in enumerate(foo):
... exec 'foo%s = %s' % (i, repr(val))
...
>>> foo3
'fum'
>>> foo[3]
'fum'
What you are asking for is a typical beginner error. Refering to the fourth item in foo
as foo3
has no advantages over writing foo[3]
. You can iterate over foo
without having to know how many items it has. You can mutate foo
if you like. You can refer to the last item in foo
as foo[-1]
without having to know it has four items. Also, the name foo3
is nonsensical. It tells the reader of the code nothing about why the items in foo
are so important that they would need a name.