Found a way to create such a list in one line)
In order to go through two lists at once, we use the zip() method. It will group the list values in pairs, and the dictionaries in the same positions in the lists will be included in the variables i and j each iteration. These dictionaries we will combine with each other.
from decimal import Decimal list1 = [{'company': 'Apple', 'rate': Decimal('1.0')}, {'company': 'Samsung', 'rate': Decimal('1.2')}] list2 = [{'company': 'Apple', 'tariff__rate': Decimal('3.47')}, {'company': 'Samsung', 'tariff__rate': Decimal('1.10')}] new_list = [dict(i, **j) for i, j in zip(list1, list2)] print(new_list)
Conclusion:
[{'rate': Decimal('1.0'), 'tariff__rate': Decimal('3.47'), 'company': 'Apple'}, {'rate': Decimal('1.2'), 'tariff__rate': Decimal('1.10'), 'company': 'Samsung'}]
You can do it this way:
from decimal import Decimal list1 = [{'company': 'Apple', 'rate': Decimal('1.0')}, {'company': 'Samsung', 'rate': Decimal('1.2')}] list2 = [{'company': 'Apple', 'tariff__rate': Decimal('3.47')}, {'company': 'Samsung', 'tariff__rate': Decimal('1.10')}] new_list = list() for i, j in zip(list1, list2): # Проходим по элементам списков i.update(j) # Записываем всё в один словарь new_list.append(i) # Добавляем словарь в список print(new_list)
Conclusion:
[{'company': 'Apple', 'tariff__rate': Decimal('3.47'), 'rate': Decimal('1.0')}, {'company': 'Samsung', 'tariff__rate': Decimal('1.10'), 'rate': Decimal('1.2')}]
In essence, the dict(i, **j) method is the same as i.update(j) , only returns a new dictionary. Write about it here .