Why is converting a list to a set faster than using just list to compute a list difference?

前端 未结 3 688
梦毁少年i
梦毁少年i 2021-01-02 05:36

Say, I wish to compute the difference of two lists C = A - B:

A = [1,2,3,4,5,6,7,8,9] 
B = [1,3,5,8,9]
C = [2,4,6,7]          #Result

3条回答
  •  臣服心动
    2021-01-02 05:55

    Average time complexity for lookup (x in S) in a set is O(1) while the same for a list is O(n).

    You can check the details at https://wiki.python.org/moin/TimeComplexity

提交回复
热议问题