The following two functions both evaluate two arrays to output the intersections of integers in the arrays passed to them. However, the second list comprehension which makes use of a set within the comprehension is said to be O(n) while purely list based comprehension is greater than O(n). As a novice to O notation that hasn't been able to find a good answer, it would great if someone could explain why using a set in the comprehension causes this.
Purely List Comprehension:
def intersection(lst1, lst2):
lst3 = [value for value in lst1 if value in lst2]
return lst3
List Comprehension Using a Set
def intersection(lst1, lst2):
temp = set(lst2)
lst3 = [value for value in lst1 if value in temp]
return lst3