0

i have a dictionary with many keys and values. i want to pop out the values, which is a df, into separate variables to use later in the code. Is there a way to do this efficiently as at the moment i am using the pop method and repeating the code for each value which for big dictionaries is not efficient? below is the code i am using but looking to make it efficient.

my_dict = {'my number 1': df, 'my number 2': df1, 'my number 3': df2, 'my number 4': df3}

df= dict.pop('my number 1')
df1= dict.pop('my number 2')
df2= dict.pop('my number 3')
df3= dict.pop('my number 4')
Zack
  • 339
  • 2
  • 12
  • 1
    Why do you need separate variables? This seems like a step back in functionality, now you can't loop over them any longer or it pass it to a function as a collection. Take a look at [variable variables](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables). – ggorlen May 29 '20 at 14:55
  • with each variable i want to apply different methods. if i loop through them i will have to use the same method. so for example each df will call a different method i have created df.output_sum(), df1.output_multiply, df2.output_divide etc. if there is a way to loop though the dictionary while applying a unique method on each value that would be ideal but i dont think there is (i maybe wrong her as i am very new to coding) – Zack May 29 '20 at 15:03
  • Sure there is. In fact, the technique you'd use to remove repetition from this code that splits the dict into individual dfs is the same one you'd apply different functions to (a _loop_). Please give more details about what you [actually want to accomplish](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) and it's likely that there's a better way to achieve it than splitting the dict into vars. – ggorlen May 29 '20 at 15:05
  • For example, the use case you show, you can easily have a list of func names, say, `["output_sum", "output_multiply"]`, then `for df, fn in zip(my_dict.values(), func_list)`, then run `getattr(df, fn)()` and you're now calling [dynamic functions](https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-by-using-its-name-a-string) on each dataframe. Even this is a bit awkward, although much less than typing everything out by hand as separate vars, so it's worth thinking back to why you need that--there could be some fundamental design flaw. – ggorlen May 29 '20 at 15:08
  • thank you! if there is a way then that would make my code much shorter. so currently once i split the dictionary out and put the values into separate variable i perform calculations on each df but the calculations for each df are different. The calculations are length as they have conditions and retrieve data from other sources so i put those calculations into separate functions which i call on the df (df.output_sum(), df1.output_multiply, df2.output_divide etc) – Zack May 29 '20 at 15:12
  • lovely let me try that now i see if it works! – Zack May 29 '20 at 15:13

0 Answers0