I'm fairly sure this will be my second thread to make me facepalm when I realise what I've missed, but oh well.

If I have a list, for example L=[0,1,2,3,4], can I take the values out so that, again for example, t1 = 0, t2=1, etc.? The easy way would obviously be t1=L[0] and so on, but what when the list is generated by user input and the program thus doesn't know the length? I can't see how I'd get a for loop to work in it as that would just keep overwriting the last t value, so in my example t will always end up as 4 with no other values.

Hope I haven't explained that too badly.

Are you saying that you want to store all of the values in the list as individual variables?

If that's the case, why is your program designed in such a way that you would need that sort of functionality? Variables are abstractions that only make any sense in the context of the source code when designing the program, so being able to create new 'variables' on the fly isn't really a valuable feature of a language unless its being used interactively.

Look at it this way. Once you've stored them all as t0, t1, t2... how are you going to access them again? In a loop? If you're going through all that trouble, why not just use a list in the first place? It doesn't make sense from a design standpoint, so the functionality isn't there. If it *is* there, it shouldn't be used.

They're being used to draw a graph and in the list they are X,Y co-ordinates. They have to be in a list for other parts of the program.

List manipulation is usually done by taking out first or last element in list. As Python lists are not really not list as much as arrays in other languages, just mixed, dynamic type, Python lists can efficiently be accesses through indexing t1=L[0] as you said.

If you need to reference list positions by name, maybe you should consider using dictionary instead of list, with those names being the hash "indexes".

Better tell one concrete difficulty that you ran into, then we can suggest better alternative.

I am not understand properly your problem but dynamically u can find out the list length
let try this
l=[1,2,3,4]
i=0
for each in l:
print l
i+=1
here value of i=no. of elements in the list

Like IsharaComix pointed out, this is not very useful ...

# add a key/variable to the local dictionary vars()
# can be done, but not very useful

mylist = [0, 1, 2, 3, 4]
n = 1
for item in mylist:
    # add variable names t1 to t5
    vars()['t'+str(n)] = item
    n += 1

# testing ...
print t1  # 0
print t3  # 2
print t5  # 4

Edited 6 Years Ago by vegaseat: n/a

This question has already been answered. Start a new discussion instead.