简体   繁体   中英

Python 3: When to use dict, when list of tuples?

I have id s of jail prisoners, for example. Each prisoner has a name.

I know how dictionarys work and I know how tuples work and I know how lists work, but sometimes I see a dictionary being used, and sometimes a list of tuples. Which one should I use in my case?

d = {
    1: "Mike",
    2: "Bob",
    3: "Tom"
}

vs

l = [
    (1, "Mike"),
    (2, "Bob"),
    (3, "Tom")
]

And to generalize the question: WHEN should I use a dict, and when should I use a list of tuples, what are the benefits of one?

You should use a list when it makes sense to store items in order. In this case it only matters that ID's are mapped to names.

A dictionary is a mapping, which means the relation between keys and values is not symmetrical. For example, it's tricky (and not always possible in the general case) to fetch a key by known value, whereas it's equally easy to filter a list (or a set, for that matter) of tuples by value of any of their items.

That being said, when choosing the data structure, it makes sense to consider how you are going to retrieve data from it. If you can see id and name as equal parts of something resembling a C struct (eg you'll need to search by any of them, etc.) then you're better off using a tuple or a collections.namedtuple . You can still put them in a list or a set depending on your need to keep it ordered.

But if id is a "special" field that is used to retrieve the rest of the info about the object, and it's guaranteed to be unique (well, "ID" means it), and you don't need internal order, and you want constant time random access -- of course use a dict.

There are two major differences between them:

  • Dictionaries are unordered, a list of tuples is. So if ordering matters, use the latter.

  • Mapping a key to a value takes constant time in a dict, doing the same in a list of tuples takes linear time. So, the larger your amount of key-value pairs, the more time it'll take to scan the list of tuples to find a match, while in a dictionary the lookup is near-instant, always.

    (If your tuples are kept in sorted order, you can reduce search time to O(log n) by using binary search; but that's still slower than constant time for dictionaries).

In most cases, you use a dict . Even if ordering is required, you can use a collections.OrderedDict instead to get the best of both worlds.

In your case, I would use a dictionary. There are several reasons you might want to consider using one.

  • The dictionary allows for usage of its API to manipulate keys and values inside it, something that would require more code to with a list of tuples.

For instance, consider this:

To get the names of the prisoners using the dictionary, you just do this:

d.values()

To do the same thing with the list of tuples you would need to do this:

names = []
for tup in l:
    names.append(tup[1])
  • The dictionary values are mutable , meaning they can be allowed to be changed. You can not do the same thing with a tuple (it is immutable ).

Eg

d[1] = 'Fotis'

To achieve the same thing with a list of tuples, you would have to substitute the tuple that you want to manipulate with a new one.

Eg

l[1] = (2, 'Max')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM