'How can I get every element from dict value and list of tuples and make one tuple from it
I Have a dict with next structure:
a = {'test_1': 1, 'test_2': 2}
and I have a second dict with list of tuples like value for key 'size_measures_multi':
b = [{'row_n': 1, 'row_section': None, 'name': 'name', 'value_1': 'qwe', 'key_1': 'test', 'value_2': [('1', 'some_value', 'some_value'), ('2', 'some_value', 'some_value')]}, {'row_n': 2, 'row_section': None, 'some_value_1': 'some_name, 'some_value_2': 'test', 'value_3': 'text', 'value_4': [('167,86', 'cm', 'netto'), ('1', 'cm', 'netto')]}]
I need get every value from dict 'a' and every first element from list of tuples from list of dict 'b' by size_measures_multi like a key and make one tuple from it. Also, I need append it to some empty list.
Result which I expect
result=[(136873521, '1', '2'), (136873857, '167,86', '1')]
Solution 1:[1]
You probably don't need to use for row in b
since you're already loop through b
using element
already. You just need to remove that and the list []
brackets in after append
function and it should work properly.
a = {'FORD#BE8Z9278-A': 136873521, 'FORD#CN1G-6L713-BC': 136873857}
b = [
{'row_n': 1, 'row_section': None, 'nomenclature_name': 'name', 'article': 'BE8Z9278-A', 'TM': 'FORD',
'size_measures_multi': [('1', 'cm', 'netto'), ('2', 'cm', 'netto')]
},
{'row_n': 2, 'row_section': None, 'nomenclature_name': 'some_name', 'article': 'CN1G-6L713-BC', 'TM': 'FORD',
'size_measures_multi': [('167,86', 'cm', 'netto'), ('1', 'cm', 'netto')]
}
]
result = []
for element in b:
new_key = "{}#{}".format(element['TM'], element['article']) # Here i get 'FORD#BE8Z9278-A' for example
result.append((a[new_key],) + tuple(item[0] for item in element['size_measures_multi']))
print(result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | VietHTran |