Python: List vs Dict for look up table

后端 未结 8 919
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 10:41

I have about 10million values that I need to put in some type of look up table, so I was wondering which would be more efficient a list or dict?

I know

8条回答
  •  心在旅途
    2020-11-22 10:51

    You want a dict.

    For (unsorted) lists in Python, the "in" operation requires O(n) time---not good when you have a large amount of data. A dict, on the other hand, is a hash table, so you can expect O(1) lookup time.

    As others have noted, you might choose a set (a special type of dict) instead, if you only have keys rather than key/value pairs.

    Related:

    • Python wiki: information on the time complexity of Python container operations.
    • SO: Python container operation time and memory complexities

提交回复
热议问题