Fastest way to check if a value exists in a list

后端 未结 12 2098
猫巷女王i
猫巷女王i 2020-11-22 00:18

What is the fastest way to know if a value exists in a list (a list with millions of values in it) and what its index is?

I know that all values in the list are uniqu

12条回答
  •  面向向阳花
    2020-11-22 00:54

    7 in a
    

    Clearest and fastest way to do it.

    You can also consider using a set, but constructing that set from your list may take more time than faster membership testing will save. The only way to be certain is to benchmark well. (this also depends on what operations you require)

提交回复
热议问题