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

I have, for example, id

prisoners. Every prisoner has a name.

I know how dictionaries 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"
}

      

against

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

      

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

+3


source to share


3 answers


You should use a list when it makes sense to keep items in order. In this case, it is only important that identifiers are mapped to names.

A dictionary is a mapping, which means that the relationship between keys and values ​​is not symmetric. For example, it is more difficult (and not always possible in general) to retrieve a key by a known value, whereas it is equally easy to filter a list (or set, for that matter) of tuples by the value of any of their elements.



That being said, when choosing a data structure, it makes sense to consider how you are going to extract data from it. If you see id

and name

as equal parts of something similar to C struct

(for example, you will need to search for any of them, etc.), then you are better off using a tuple or collections.namedtuple

. You can put them in a list or a set, depending on your need, so that it is ordered.

But if id

is a "special" field that is used to retrieve the rest of the information about an object, and it is guaranteed to be unique (well, that means "identifier"), and you don't need internal ordering, and you want constant random access over time - of course, use a dict.

+6


source


There are two main differences between them:

  • Dictionaries are unordered, list of tuples is. Therefore, if you order questions, use the latter.

  • Key-to-value mapping takes constant time in a dict, doing the same in a list of tuples takes linear time. So, the larger the number of key-value pairs, the longer it will take to scan the list of tuples to find a match, while in a dictionary, lookups are almost instantaneous, always.

    (If your tuples are stored in ordered order, you can reduce the search time to O (log n) with a binary search, but even slower than constant time for dictionaries).



Most of the time you use dict

. Even if ordering is required, you can use collections.OrderedDict

to get the best of both worlds.

+5


source


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

  • A dictionary allows its API to be used to manipulate keys and values ​​within it, which would require more code with a list of tuples.

For example, consider the following:

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

d.values()

      

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

names = []
for tup in l:
    names.append(tup[1])

      

  • Dictionary values ​​change , which means you can change them. You cannot do the same with a tuple (it is immutable ).

eg

d[1] = 'Fotis'

      

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

eg

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

      

+2


source







All Articles