How to efficiently get the mean of the elements in two list of lists in Python

EmJ

I have two lists as follows.

mylist1 = [["lemon", 0.1], ["egg", 0.1], ["muffin", 0.3], ["chocolate", 0.5]]
mylist2 = [["chocolate", 0.5], ["milk", 0.2], ["carrot", 0.8], ["egg", 0.8]]

I want to get the mean of the common elements in the two lists as follows.

myoutput = [["chocolate", 0.5], ["egg", 0.45]]

My current code is as follows

for item1 in mylist1:
    for item2 in mylist2:
        if item1[0] == item2[0]:
             print(np.mean([item1[1], item2[1]]))

However, since there are two for loops (O(n^2) complexity) this is very inefficient for very long lists. I am wondering if there is more standard/efficient way of doing this in Python.

Adam.Er8

You can do it in O(n) (single pass over each list) by converting 1 to a dict, then per item in the 2nd list access that dict (in O(1)), like this:

mylist1 = [["lemon", 0.1], ["egg", 0.1], ["muffin", 0.3], ["chocolate", 0.5]]
mylist2 = [["chocolate", 0.5], ["milk", 0.2], ["carrot", 0.8], ["egg", 0.8]]

l1_as_dict = dict(mylist1)

myoutput = []
for item,price2 in mylist2:
    if item in l1_as_dict:
        price1 = l1_as_dict[item]
        myoutput.append([item, (price1+price2)/2])

print(myoutput)

Output:

[['chocolate', 0.5], ['egg', 0.45]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to efficiently get the mean of the elements in two list of lists in Python

From Java

How to efficiently get count for item in list of lists in python

From Dev

How to efficiently get count for item in list of lists in python

From Dev

How to compare two lists in Python efficiently?

From Dev

How to efficiently find the indices of matching elements in two lists

From Java

How to efficiently search list elements in a string in python

From Dev

How to efficiently search list elements in a string in python

From Dev

python mean of list of lists

From Dev

python get last 5 elements in list of lists

From Dev

python compare a list of lists efficiently

From Dev

python compare a list of lists efficiently

From Dev

How to select elements in the intersection of two lists in Python

From Dev

How to efficiently tally co-occurrences in python list of lists

From Dev

Efficiently adding elements to a list in python

From Dev

Python: how to simultaneously add two lists to a list?

From Dev

How to efficiently remove the same-length elements from a list in python

From Dev

python how to efficiently cycle through few elements in a list

From Dev

finding item in two lists efficiently Python

From Dev

Efficiently combining values of two lists in Python

From Dev

How to merge two lists? Preserving identical list elements for set manipulation

From Dev

How to map two lists of objects by id efficiently

From Dev

Get a list of combinations of lists' elements

From Dev

Efficiently comparing the first item in each list of two large list of lists?

From Dev

R - save as data.frame all elements of a list of lists efficiently

From Dev

R - save as data.frame all elements of a list of lists efficiently

From Dev

How to get lists of indices to unique values efficiently?

From Dev

How to check for common elements between two lists in python

From Dev

How to add repeating occurences of elements in two lists in python

From Dev

How to merge two elements in a list in Python

Related Related

  1. 1

    How to efficiently get the mean of the elements in two list of lists in Python

  2. 2

    How to efficiently get count for item in list of lists in python

  3. 3

    How to efficiently get count for item in list of lists in python

  4. 4

    How to compare two lists in Python efficiently?

  5. 5

    How to efficiently find the indices of matching elements in two lists

  6. 6

    How to efficiently search list elements in a string in python

  7. 7

    How to efficiently search list elements in a string in python

  8. 8

    python mean of list of lists

  9. 9

    python get last 5 elements in list of lists

  10. 10

    python compare a list of lists efficiently

  11. 11

    python compare a list of lists efficiently

  12. 12

    How to select elements in the intersection of two lists in Python

  13. 13

    How to efficiently tally co-occurrences in python list of lists

  14. 14

    Efficiently adding elements to a list in python

  15. 15

    Python: how to simultaneously add two lists to a list?

  16. 16

    How to efficiently remove the same-length elements from a list in python

  17. 17

    python how to efficiently cycle through few elements in a list

  18. 18

    finding item in two lists efficiently Python

  19. 19

    Efficiently combining values of two lists in Python

  20. 20

    How to merge two lists? Preserving identical list elements for set manipulation

  21. 21

    How to map two lists of objects by id efficiently

  22. 22

    Get a list of combinations of lists' elements

  23. 23

    Efficiently comparing the first item in each list of two large list of lists?

  24. 24

    R - save as data.frame all elements of a list of lists efficiently

  25. 25

    R - save as data.frame all elements of a list of lists efficiently

  26. 26

    How to get lists of indices to unique values efficiently?

  27. 27

    How to check for common elements between two lists in python

  28. 28

    How to add repeating occurences of elements in two lists in python

  29. 29

    How to merge two elements in a list in Python

HotTag

Archive