There are two lists of dictionaries:

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')}] 

How to unite everything in one list of dictionaries, adding one more factor for the corresponding company.
It should turn out like this:

 new_list = [{'company': 'Apple', 'tariff__rate': Decimal('3.47'), 'rate': Decimal('1.0')}, {'company': 'Samsung', 'tariff__rate': Decimal('1.10'), 'rate': Decimal('1.2')}] 

I use python3.5.3. Thanks in advance for your help!

    2 answers 2

    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 .

    • I liked the way in one line, thanks!) - Fill
    • Add a description of how the zip will work in this case - kitscribe

    If the indices coincide and the dimension of the array is the same, this can be done in the following 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')}] for x in range(len(list1)): list1[x].update(list2[x]) print(list1[x]) # {'company': 'Apple', 'rate': Decimal('1.0'), 'tariff__rate': Decimal('3.47')} # {'company': 'Samsung', 'rate': Decimal('1.2'), 'tariff__rate': Decimal('1.10')} 

    The list1 itself now looks like this:

     [{'tariff__rate': Decimal('3.47'), 'rate': Decimal('1.0'), 'company': 'Apple'}, {'tariff__rate': Decimal('1.10'), 'rate': Decimal('1.2'), 'company': 'Samsung'}] 

    If it is not known whether the indices match, then we can iterate over all the values ​​and check whether the company names match:

     for x in range(len(list1)): for y in range(len(list2)): if list1[x]['company'] == list2[y]['company']: list1[x].update(list2[y]) print(list1[x]) # {'rate': Decimal('1.0'), 'company': 'Apple', 'tariff__rate': Decimal('3.47')} # {'rate': Decimal('1.2'), 'company': 'Samsung', 'tariff__rate': Decimal('1.10')} 

    list1 looks like this:

     [{'rate': Decimal('1.0'), 'company': 'Apple', 'tariff__rate': Decimal('3.47')}, {'rate': Decimal('1.2'), 'company': 'Samsung', 'tariff__rate': Decimal('1.10')}] 

    It is important to remember that the order in the lists is irrelevant and may change .