-1

Info

I'm trying to separate a list of independent variables, but my list is changing (the number of things on the list is changing). I tried

var1, var2, var3 = mylist

but it works only when my_list have 3 "items", but it won't have 3 items every time, sometimes the amount will change. I'm trying to make a discord bot that collects ids from voice channels and uses them in command "client.get_channel()" which only accepts one id. I will use it to check who is connected to my voice channels.

Goal

I want to separate a list to independent variables, even when the list sometimes has a different amount/quantity of "items".


My idea for solution

I was thinking about making a script that would count items (ex. len(my_list)) on my list and make enough variables, but I don't know how to do it.

double-beep
  • 5,031
  • 17
  • 33
  • 41
RiveN
  • 2,595
  • 11
  • 13
  • 26
  • 1
    What's your purpose behind doing that? Knowing that might help give you a solution. What can you do with separate variables that you cannot with a list? – Aziz Sonawalla Aug 31 '20 at 15:53
  • 1
    Why do you want them separated out into variables? Why not just use the list as is? What *problem are you actually trying to solve*? – Karl Knechtel Aug 31 '20 at 15:54
  • 1
    You *already have* names for each of the items in the list. They are `mylist[0]`, `mylist[1]`, `mylist[2]`, and so on. – jasonharper Aug 31 '20 at 15:54
  • 1
    Do you want the first three elements as separate variables, no matter how many elements the list has, or do you want _n_ separate variables for each element in the list? For the first case, use `a, b, c = lst[:3]` or `a, b, c, *_ = lst`, and for the latter case, just use the list itself. – tobias_k Aug 31 '20 at 15:57
  • As I see it: first you have problem how to create variables dynamically. After solving that you will face next problem - how to access variables created dynamically..... – Aivar Paalberg Aug 31 '20 at 16:00
  • 2
    Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) tl;dr you actually want a list – wjandrea Aug 31 '20 at 16:02
  • 1
    This appears to be an [XY Problem](https://en.wikipedia.org/wiki/XY_problem). – Prune Aug 31 '20 at 16:13
  • I'm making discord bot which gives me channels ids which I need to use separately in my code to count members in each channel. – RiveN Aug 31 '20 at 16:40

2 Answers2

1

Converting elements of a list to separate variables won't achieve anything useful. If you want to reference them individually, then you can already do that:

mylist[0]
mylist[1]
... #and so on

If you're hoping to assign variables for these values for better readability (i.e. maybe mylist[0] always refers to a user's name) then use objects instead of lists and call user.name

Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6
0

The problem with what you're proposing is that you're dynamically creating local variables. If you do that, you can't be deterministic in your code. If you have 4 elements in a list and you create var1, var2, var3, var4, and then later there are 5 elements, what do you do with your code?

Having said that, you can dynamically create variables at runtime. It's just probably not a good idea:

>>> my_list = [1,2,3,4]

I have a list of 4 items. I can create var_0, var_1, etc based on it, at runtime, by assigning elements to the locals() dict:

>>> for i, element in enumerate(my_list):
...     locals()[f'var_{i}'] = element
... 
>>> var_0
1
>>> var_1
2
>>> var_2
3
>>> var_3
4
>>> var_4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'var_4' is not defined

Notice that if my list were one element longer, var_4 would have been defined.

Ken Kinder
  • 12,654
  • 6
  • 50
  • 70