0

I have 8 dictionaries intialized in the same way

dicc_1={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0}
dicc_2={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0}    
dicc_3={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0}
dicc_4={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0}
dicc_5={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0}
dicc_6={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0}
dicc_7={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0}
dicc_8={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0}

Question1: Is there a better way to do this? Because if I need more dictionaries it will make the script very large It would be nice if there is a solution like this:

dicc_1,dicc_2,dicc_3,dicc_4,dicc_5,dicc_6,dicc_7,dicc_8={"Area":0,"Age":0,"Weight":0, "Height":0, "All":0} * 8

But it doesn't work

Question 2: Is there a way to change the keys if I pass a list of keys? Like this function that doesn't work too

def myFunc(ListOfKeys):
   dicc_1,dicc_2,dicc_3,dicc_4,dicc_5,dicc_6,dicc_7,dicc_8={ListOfKeys[0]:0,ListOfKeys[1]:0,ListOfKeys[2]:0, ListOfKeys[3]:0, ListOfKeys[4]:0} * 8
   "Do stuff with dictionaries"
   "Return all dictionaries"

So this evaluation:

myFunc(ListOfKeys=["Area","Age","Weight","Height","All"])

Would intialize dictionaries in this way:

dicc_1={"Area":0,"Age":0,"Weight":0,"Height":0,"All":0}
dicc_2={"Area":0,"Age":0,"Weight":0,"Height":0,"All":0}
dicc_3={"Area":0,"Age":0,"Weight":0,"Height":0,"All":0}
dicc_4={"Area":0,"Age":0,"Weight":0,"Height":0,"All":0}
dicc_5={"Area":0,"Age":0,"Weight":0,"Height":0,"All":0}
dicc_6={"Area":0,"Age":0,"Weight":0,"Height":0,"All":0}
dicc_7={"Area":0,"Age":0,"Weight":0,"Height":0,"All":0}
dicc_8={"Area":0,"Age":0,"Weight":0,"Height":0,"All":0}

And this evaluation:

myFunc(ListOfKeys=["Car","Train","Ship"])

Would intialize dictionaries in this way:

dicc_1={"Car":0,"Train":0,"Ship":0}
dicc_2={"Car":0,"Train":0,"Ship":0}    
dicc_3={"Car":0,"Train":0,"Ship":0}
dicc_4={"Car":0,"Train":0,"Ship":0}
dicc_5={"Car":0,"Train":0,"Ship":0}
dicc_6={"Car":0,"Train":0,"Ship":0}
dicc_7={"Car":0,"Train":0,"Ship":0}
dicc_8={"Car":0,"Train":0,"Ship":0}
Víctor
  • 117
  • 2
  • 9

1 Answers1

1

Own Answer1. Thanks to: Python - Initializing Multiple Lists/Line

dicc_1, dicc_2, dicc_3, dicc_4, dicc_5, dicc_6, dicc_7, dicc_8= ({"Area":0,"Age":0,"Weight":0, "Height":0, "All":0} for i in range(8))

Own Answer2. Thanks to: How to initialize a dict with keys from a list and empty value in Python?

def myFunc(ListofKeys):
    dicc_1, dicc_2, dicc_3, dicc_4, dicc_5, dicc_6, dicc_7, dicc_8 =((dict.fromkeys(ListofKeys,0))  for i in range(8))
    dicc_out={"dicc_1":dicc_1,"dicc_2":dicc_2,"dicc_3":dicc_3,"dicc_4":dicc_4,"dicc_5":dicc_5,"dicc_6":dicc_6,"dicc_7":dicc_7,"dicc_8":dicc_8}
    return(dicc_out)
Víctor
  • 117
  • 2
  • 9
  • Sorry I'm new using StackOverflow and I edited the question and I lost an interesting answer that someone left here. I apologyze for that – Víctor Aug 08 '20 at 19:07