3

I am very new at python, so this might sound stupid. I searched but I did not find a solution.

I have a function in python called ExcRng. What kind of for loop can I do with the function so it stores the values in independents variables? I do not wish to store them in a list, but as an independent variable.

I am looking for something like this.

EDIT* So to clarify, the inputs "a" to "n" are strings. The output df1, df2...dfn are DataFrames.

List=[a,b,c,d,e...,n]

for i in List:
    df1=ExcRange(i)
    df2=ExcRng(i)
    ...
    dfn=ExcRng(in)
1
  • 1
    I think storing in a dictionary would be a reasonable solution here. You can create an empty dictionary before your loop and add key/value pairs in each iteration. Commented May 8, 2020 at 2:42

5 Answers 5

2

You can access each element just by their index number. But if you want to have independent variables just make one venerable for one element of the List, you don't need for loop for that.

List = [a,b,c,d,e,f]

#for the first element a
df1 = ExcRange(List[0])
#for the second element b
df2 = ExcRange(List[1])
#etc List[5] = f

I would recommend you to always use list instead of multiple values because you're saving memory and your code is looking way cleaner. You can learn them, they are pretty easy

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

1 Comment

This is the output I am looking for. But is there a way to automate the output of df1, df2...dfn? In case the List=[a,b,c....n]
0

Try a dictionary:

List=[a,b,c,d,e,f]
Dict={}
for c,i in enumerate(List):
    Dict["df"+str(i+1)] = ExcRng(i)

Now, when you want to get your variable, you can use Dict["df1"]

1 Comment

Using List and Dict as variable names is painful to see :')
0

In Python fucntions can return more than one object. You can do :

ExcRange (i) {  
 //Your code                                                            
   return df1,df2,,,,,,,,dfn
}

In Your main Code : for i in List df1,df2,,,,dfn= ExcRange (i)

Comments

0

It's not very usual to do what you're trying, so I recommend you take some time to learn about dictionaries and other data structures that might help you. Said that, I see a way of achieving something like this:

The python method exec allows dynamic execution of python code, which in our case means running a line of code from a string.

So, if you'd normally use a = 1 to declare/attribute a variable named a with value 1, you can achieve the same result with exec("a = 1").

This allows you to use formatted strings to create variables from a list of variable names given as strings:

var_names = ["a", "b", "c", "d"]

for var in var_names:
  exec("{} = {}".format(var, var_names.index(var)*2))

print(a)
print(b)
print(c)
print(d)

Once we're attributing to the variable the index of the name in the list multiplied by two, this should be the output:

0
2
4
6

It wasn't exactly clear to me what you want to do, so reading again the question I got the impression you might want the values returned by the ExcRange method to be stored on separate variables, not on a list, and if that's the case, good news: python syntax sugar is here to help!

def method():
    return 1, 3, 5, 7

a, b, c, d = method()

print(a) # a = 1
print(b) # b = 3
print(c) # c = 5
print(d) # d = 7

On this case, the method returns a list with 4 elements [1,3,5,7], but if you assign the result to exactly 4 variables, Python unpacks it for you.

Note that if you assign the method to a single variable, this variable will store the list itself.

a, b, c, d = method()

print(a) # a = [1, 3, 5, 7]

Comments

0
values = [a,b,c,d,e...,n]
dfs = {f'df{i+1}': ExcRng(v) for i, v in enumerate(values)}
print(dfs['df1'])
...
<Your dataframe here>

Comments

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.