[SOLVED] Crossover of two Lists in Python

Issue

I have 2 lists with tens of thousands of records as follows:

list1=[
"c:\\user",
"C:\\Windows\\app\\Prog\\folder2\\folder3",
"C:\\Windows\\app\\Prog\\folder2",
"C:\\Intel\\Profiles\\user3\\se",
"C:\\Win\\add\\folder1"
]

list2=[
"C:\\Win\\add",
"C:\\Windows\\app\\Prog",
"C:\\Intel\\Profiles"
]

I need to cross these two lists and put the result in a dictionary list like so:

dic=[
  {"k1": "c:\\user","k2": ""},
  {"k1": "C:\\Windows\\app\\Prog\\folder2\\folder3", "k2": "C:\\Windows\\app\\Prog"},
  {"k1": "C:\\Windows\\app\\Prog\\folder2", "k2": "C:\\Windows\\app\\Prog"},
  {"k1": "C:\\Intel\\Profiles\\user3\\se", "k2": "C:\\Intel\\Profiles"}
  {"k1": "C:\\Win\\add\\folder1", "k2": "C:\\Win\\add"}
]

I solved it like this, but the execution is very slow because the lists can have hundreds of thousands of records.

def mergelists(list1,list2):
   dic=[]
   for i in range(len(list1)):
        fitem1=list1[i]
        result = [element for element in list2 if element in fitem1]
        row = {"k1": fitem1, "k2": result[0]}
        dic.append(row)
   return (dic)

how to increase execution speed?

Solution

One obvious way to speed this up is to change

result = [element for element in list2 if element in fitem1]
row = {"k1": fitem1, "k2": result[0]}

to

result = next(element for element in list2 if element in fitem1)
row = {"k1": fitem1, "k2": result}

because currently the code continues to search the whole list2 for elements that match fitem1, even if one has already been found.

The next function will return as soon as one element has been found and will skip searching the rest of list2.

Answered By – mkrieger1

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *