0

Suppose I have the following list:

['a','b','c','d']

And multiple dictionaries of the form

{'a':val1,'b':val2,'c':val3,'d':val4}
{'a':val5,'b':val6,'c':val7,'d':val8}

I want to create lists for each element in the list, like below:

a=[val1,val5]
b=[val2,val6]
c=[val3,val7]
d=[val4,val8]

Note : I could have more than 4 elements in my initial list.

hegdep
  • 596
  • 1
  • 6
  • 16
  • 1
    What happens if you have 100 elements in the list, are you going to create 100 variables? Even if you could, you shouldn't do that. Is better if you create a dictionary, and easier too. – Dani Mesejo Oct 14 '19 at 20:31
  • ^ just `r = {x: [d1.get(x,''), d2.get(x,'')] for x in l}` – splash58 Oct 14 '19 at 20:44
  • @splash58 What about if I have 100 dictionaries? – hegdep Oct 14 '19 at 20:57
  • 1
    Make a list of dictionaries `d = [{'a':'val1','b':'val2','c':'val3','d':'val4'}, {'a':'val5','b':'val6','c':'val7','d':'val8'}]; r = {x: [i.get(x,'') for i in d] for x in l}` – splash58 Oct 14 '19 at 20:59

0 Answers0