+ 2

Can i affect all items of a list into multiples variables ?

I have a list like [1,2,3,4] and i Want to have all values into variables like a=1 b=2 c=3 d=4 But the lenght of my list can change

28th Dec 2019, 9:28 PM
Dorian
31 Respostas
+ 6
Dorian I did mistake. My variant is only output. Sorry. HonFu , your answer made me reread the condition. Thank you!
28th Dec 2019, 9:46 PM
Petr
+ 6
Dorian here is your answer.this code create a multiple variable by separating the each value of list without knowing the length of the list. ll = ['xx','yy','zz','pp','jj'] for n, val in enumerate(ll): globals()["var%d"%n] = val print(var4) #output=jj print(var3) #output=pp print(var) #output=xx #etc..... But this is not a good practice if you are creating multiple variables in these manners.let suppose if you have 1000 variables what will you do with it.
30th Dec 2019, 4:15 AM
Maninder $ingh
Maninder $ingh - avatar
+ 5
Dorian My be function ord(char) help you?
28th Dec 2019, 9:32 PM
Petr
+ 5
Dorian Ord is ASCII code of char
28th Dec 2019, 9:34 PM
Petr
+ 5
Maninder $ingh I didn't know you could do it without exec(), good one!
30th Dec 2019, 4:20 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Dorian Try so: s=[1,5,8,9,11] for i in range(len(s)): print(str(s[i])+" - "+chr(ord("a")+i))
28th Dec 2019, 9:41 PM
Petr
+ 4
Dorian You can connect my answer and from HonFu. Create a new dictionary
28th Dec 2019, 9:51 PM
Petr
+ 4
Aymane Boukrouh with the help of globals function.
30th Dec 2019, 4:21 AM
Maninder $ingh
Maninder $ingh - avatar
+ 3
Yes HonFu but i don't know how in avance the lenght of the list or dictionnary
28th Dec 2019, 9:51 PM
Dorian
+ 3
rodwynnejones my code can very easily be tweaked to as many variables as you want, with less than 5 lines of code
28th Dec 2019, 10:29 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Dorian, what sort of operation do you need to perform on the user-provided values? Depending on what you want to do, there might be different good options. Also, why do you need the values to have names in the first place?
28th Dec 2019, 10:38 PM
HonFu
HonFu - avatar
+ 2
Have you thought about using a dictionary? It is not exactly the same, but you can give names to your values. d = {'a': 5; 'b': 7} print(d['a']) Output: 5
28th Dec 2019, 9:43 PM
HonFu
HonFu - avatar
+ 2
Thank you Petr, but Indeed values are not assigned
28th Dec 2019, 9:49 PM
Dorian
28th Dec 2019, 9:52 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
global a global b global c global d mylist = [1,2,3,4] if len(mylist) == 2: a, b = mylist elif len(mylist) == 3: a, b, c = mylist elif len(mylist) == 4: a, b, c, d = mylist # and so on. print(a, b, c, d) #note: it does work without the global declaration, but my IDE gives me a warning..so I've included it.
28th Dec 2019, 10:03 PM
rodwynnejones
rodwynnejones - avatar
+ 2
rodwynnejones that's never a good stategy, what about a list of 1000 items ? Also, why use global variables ?
28th Dec 2019, 10:04 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Thank you Aymane your programme work, it's because we actually have a project to do. In the first question, we have defined a function in which we had already put our lists. However, for the second one, we need to modify these lists. So we actually decided to ask the user to put his lists. But we donā€™t know how can we use the same program as before because we had defined a variable for each item of the list. Consequently, do we need to change the program or do we have something to help us to define the variables.
28th Dec 2019, 10:15 PM
Dorian
+ 2
Dorian well I hope this was what you needed. In naming, it is better to use a1, a2, a3... instead of a, b, c... because it would be more readable
28th Dec 2019, 10:18 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
@Aymane Boukrouh I think I miss interpreted the warning my IDE was giving me (I though it was telling me that the variables were local to the "if elif" statements). 1000 items?..try your code with more than 26 items.
28th Dec 2019, 10:28 PM
rodwynnejones
rodwynnejones - avatar
28th Dec 2019, 11:27 PM
Coding Cat
Coding Cat - avatar