Which is faster and why? Set or List?

前端 未结 3 2048
误落风尘
误落风尘 2020-11-30 10:09

Lets say that I have a graph and want to see if b in N[a]. Which is the faster implementation and why?

a, b = range(2)
N = [set([b]), set([a,b])         


        
3条回答
  •  悲&欢浪女
    2020-11-30 10:26

    Set ( I mean a hash based set like HashSet) is much faster than List to lookup for a value. List has to go sequentially to find out if the value exists. HashSet can directly jump and locate the bucket and look up for a value almost in a constant time.

提交回复
热议问题