List with many dictionaries VS dictionary with few lists?

后端 未结 6 2273
你的背包
你的背包 2020-12-29 02:40

I am doing some exercises with datasets like so:

List with many dictionaries

users = [
    {\"id\": 0, \"name\": \"Ashley\"},
    {\         


        
6条回答
  •  长情又很酷
    2020-12-29 03:23

    Time complexity for the lookups in -

    • List - O(n)
    • Dicts - O(1)

    But that wouldn't hurt much if your data isn't that big and also modern day processors are quite efficient.
    You should go with the one in which the lookup is syntactically cleaner and readable(readability matters).
    The first option is quite appropriate as the variable is a collection of users(which have been assigned an id) while the second would be just a collection of usernames and ids.

提交回复
热议问题