I have two lists(l,s)
. Now I want to create a dictionary where I want to add the index values, values of the lists to a dictionary keys that looks like below.
{"value_s": index value of s, "Name_s": value of list s,'value_l': index value of l, 'value_s': value of list s}
Suppose list l have four values and s have two values, then based on the length of list l the number of index's should be added to s. If the length of l is 4 then the for loop should add one to four to value_s
and the value_l
should be 0 for the first run. This might be confusing for better understanding refer to the code and the excepted output below:-
l = ['a','b','c','d']
s = ['q','w']
The above are the two lists. Now the excepted output is:-
[{'value_s': '0', 'Name_s': 'q', 'value_l': 0, 'Name_s': 'a'},
{'value_s': '0', 'Name_s': 'q', 'value_l': 1, 'Name_s': 'b'},
{'value_s': '0', 'Name_s': 'q', 'value_l': 2, 'Name_s': 'c'},
{'value_s': '0', 'Name_s': 'q', 'value_l': 3, 'Name_s': 'd'},
{'value_s': '1', 'Name_s': 'w', 'value_l': 0, 'Name_s': 'a'},
{'value_s': '1', 'Name_s': 'w', 'value_l': 1, 'Name_s': 'b'},
{'value_s': '1', 'Name_s': 'w', 'value_l': 2, 'Name_s': 'c'},
{'value_s': '1', 'Name_s': 'w', 'value_l': 3, 'Name_s': 'd'},
]
All though I tried implementing the basic part of inserting the index and values. The idea what I did is not a good way to do
f=[]
for idx, val in enumerate(s):
for index, value in enumerate(l):
name = {"value_s":idx, "Name_s": val,'value_l':index, 'value_s':value }
f.append(name)
This gives me:-
[{'value_s': 'a', 'Name_s': 'q', 'value_l': 0},
{'value_s': 'b', 'Name_s': 'q', 'value_l': 1},
{'value_s': 'c', 'Name_s': 'q', 'value_l': 2},
{'value_s': 'd', 'Name_s': 'q', 'value_l': 3},
{'value_s': 'a', 'Name_s': 'w', 'value_l': 0},
{'value_s': 'b', 'Name_s': 'w', 'value_l': 1},
{'value_s': 'c', 'Name_s': 'w', 'value_l': 2},
{'value_s': 'd', 'Name_s': 'w', 'value_l': 3}]
Is there any good way to achieve the excepted output?
CodePudding user response:
l = ['a','b','c','d']
s = ['q','w']
f=[]
for idx, val in enumerate(l):
for index, value in enumerate(s):
name = { "value_s":index, "Name_s": value, 'value_l':idx, 'name_l':val }
f.append(name)
print(f)