-1

I am working in Python trying to write a function using a list of variables. Here is the data I am working with:

material_list=['leather', 'canvas', 'nylon']

def materialz(MAT):

   MAT=support.loc[(material==MAT)].sum()

for i in enumerate(material_list):

   materialz(i)

What I am looking for is to pass in each of the items in the list to the function to produce global variables.

leather=

canvas=

nylon=

Any help would be appreciated!

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Shawn Jamal
  • 170
  • 8

2 Answers2

1

You could create a dictionary and dynamically assign the key-value pairs there. Such as:

material_list=['leather', 'canvas', 'nylon']
material_dict={}
for i in enumerate(material_list):
    material_dict[i]=value #Where i would be the key and value the value in the key-value pair
Kintori
  • 51
  • 1
  • 5
-2

you can use exec

var = 'hello'

output = hello

exec('var = "world"')
print(var)

output = world

  • Using `exec()` is often a safety hazard and creating variables on-the-fly is generally considered a bad idea, see [Why you don't want to dynamically create variables](https://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html). – martineau Mar 08 '21 at 21:19