1

Is there any possibility of creating a list of variables/names* that have not been defined yet, and then loop through the list at a later stage to define them?

Like this:

varList = [varA, varB, varC]
for var in varList:
    var = 0
print(varList)

>>>[0, 0, 0]

The reason I'm asking is because I have a project where I could hypothetically batch fill 40+ variables/names* this way by looping through a Pandas series*. Unfortunately Python doesn't seem to allow undefined variables in a list. Does anyone have a creative workaround?

EDIT: Since you asked for the specific problem, here goes:

I have a Pandas series that looks like this (excuse the Swedish):

print(Elanv)

>>>
Förb. KVV PTP                 5653,021978
Förb. KVV Skogsflis                     0
Förb. KVV Återvinningsflis    337,1416119
Förb. KVV Eo1                         6,1
Förb. HVC Återvinningsflis           1848
Name: Elanv, dtype: object

I want to store each value in this array to a set of new variables/names*, the names of which I want to control. For example, I want the new variable/name* containing the first value to be called "förbKVVptp", the second one "förbKVVsflis", and so forth.

The "normal" option is to assign each variable manually, like this:

förbKVVptp, förbKVVsflis, förbKVVåflis = Elanv.iloc[0], Elanv.iloc[1], Elanv.iloc[2] .... 

But that creates a not so nice looking long bunch of code just to name variables/names*. Instead I thought I could do something like this (obviously with all the variables/names*, not just the first three) which looks and feels cleaner:

varList = [förbKVVptp, förbKVVsflis, förbKVVåflis]
for i, var in enumerate(varList): var = Elanv.iloc[i]

print(varList)

>>>[5653,021978, 0, 337,1416119]

Obviously this becomes pointless if I have to write the name of my new variables/names* twice (first to define them, then to put them inside the varList) so that was why I asked.

7
  • 3
    No. You do not define variables in Python. To be precise, there are no variables in Python. Only "names" which serve as references to objects Commented Oct 23, 2018 at 10:54
  • looping through a Pandas array. What's a Pandas array? Pandas is designed to avoid Python-level loops. Commented Oct 23, 2018 at 10:56
  • 7
    This is an XY problem. Ask about your problem, not an attempted solution Commented Oct 23, 2018 at 10:57
  • Your problem still isn't clear to me after your edit. Are you trying to create a bunch of variables, or a list? Commented Oct 23, 2018 at 11:28
  • Sorry for that. The list is just the method for trying to create the bunch of variables (so that I can loop through them). Commented Oct 23, 2018 at 11:33

3 Answers 3

6

You cannot create uninitialized variables in python. Python doesn't really have variables, it has names referring to values. An uninitialized variable would be a name that doesn't refer to a value - so basically just a string:

varList = ['förbKVVptp', 'förbKVVsflis', 'förbKVVåflis']

You can turn these strings into variables by associating them with a value. One of the ways to do that is via globals:

for i, varname in enumerate(varList):
    globals()[varname] = Elanv.iloc[i]

However, dynamically creating variables like this is often a code smell. Consider storing the values in a dictionary or list instead:

my_vars_dict = {
    'förbKVVptp': Elanv.iloc[0],
    'förbKVVsflis': Elanv.iloc[1],
    'förbKVVåflis': Elanv.iloc[2]
}

my_vars_list = [Elanv.iloc[0], Elanv.iloc[1], Elanv.iloc[2]]

See also How do I create a variable number of variables?.

Sign up to request clarification or add additional context in comments.

1 Comment

As suspected! Thank you, this is the answer I was looking for! Now I have both the option to go smelly code, or just do it the more pythonic way.
2
  • The answer to your question is that you can not have undefined variables in a list.
  • My solution is specific to solving this part of your problem The reason I'm asking is that I have a project where I could hypothetically batch fill over 100 arrays this way by looping through a Pandas array.
  • Below solution prefills the list with None and then you can change the values in the list.

Code:

varList = [None]*3
for i in range(len(varList)):
    varList[i] = 0
print(varList)  

Output:
[0, 0, 0]

13 Comments

Why even bother with creating a list with 3 None and then setting them to 0? just do [0] * 3
I just answered the question... He wanted like that so I did it
@AndrewMcDowell Setting something to None does not mean it "had not been defined". It means that it had been defined and set to None.
@AndrewMcDowell More over, the answer to the question is no. This answer suggests that it is possible, which is wrong.
Updated the question, guys. Thanks for showing interest, and sorry for not giving you the specific problem. Please be kind to one another!
|
-2

So something you are trying to do in your example that won't do what you expect, is how you are trying to modify the list:

for var in varList:
    var = 0

When you do var = 0, it won't change the list, nor the values of varA, varB, varC (if they were defined.)

Similarly, the following won't change the value of the list. It will just change the value of var.

var = mylist[0]
var = 1

To change the value of the list, you need to do an assignment expression on an indexed item on the list:

mylist = [None, None, None]
for i in range(len(mylist)):
    mylist[i] = 0

print(mylist)

Note that by creating a list with empty slots before assigning the value is inefficient and not pythonic. A better way would be to just iterate through the source values, and append them to a list, or even better, use a list comprehension.

6 Comments

that is roughly the same answer as the other one
@Jean-FrançoisFabre - Yes, but I've tried to explain why his approach is wrong, and suggest better approaches.
@GaryvanderMerwe OPs approach is wrong mostly because it is impossible in Python. Any other answer is a (wrong and awkward) workaround.
@DeepSpace hence I'm trying to explain why his approach is wrong, and what a better approach is.
@GaryvanderMerwe I understand, but since OP didn't even explain us what his true problem here is (something to do with pandas), this question can not be correctly answered.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.