3

I have a use case where I need to be storing a pair of values in Python where both values will need to be updated frequently.

Do I use a list or a tuple or something else? On one hand, a list is easier to update, since tuples are immutable and I will need to create a new tuple every time I update any one of the values. However, since I will not be appending anything to the list, and the size is fixed at 2, it feels like a tuple may be a better representation of what the object actually is.

Any insight? Thanks!

user202925
  • 521
  • 2
  • 5
  • 16

4 Answers4

2

Make a pair class?

class pair(object):
     def __init__(self,first,second):
         self.update(first,second)
     def update(self,first,second):
         self.first = first
         self.second = second

This clarifies it's only two objects, and that updates are welcome. If you want basic arithmetic with other pairs/lists/tuples you can implement that easily enough.

kabanus
  • 24,623
  • 6
  • 41
  • 74
2

Well there are several ways You could go about creating something like this, but I'd stick with a list. There's no reason to over think it.

I will not be appending anything to the list, and the size is fixed at 2, it feels like a tuple may be a better representation of what the object actually is.

No. List do not have to change size. List can stay a constant size through your entire program. And the list has the distinct advantage, that it supports item assignment.

You could also consider looking into a collections.namedtuple if your looking for a light weight solution to @kabanus's. In fact, the official Python documentation of namedtuple shows an example with namedtuple using points:

>>> # Basic example
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(11, y=22)     # instantiate with positional or keyword arguments
>>> p[0] + p[1]             # indexable like the plain tuple (11, 22)
33
>>> x, y = p                # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y               # fields also accessible by name
33
>>> p                       # readable __repr__ with a name=value style
Point(x=11, y=22)
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
0

If you care about performance and want to name the fields, use __slots__:

class datum(object):
    __slots__ = ['x', 'y']

Now you have a simple, high-performance, compact type which does not require dictionary lookup for field access.

But if you care about performance and do not want to name the fields, you may as well stick with list.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

A list is perfectly sufficient here. You shouldn't discount it as a possibility just because you always have the same amount of data.

I don't feel that using a tuple is appropriate here, because 1) generally, you use immutable types for data that doesn't change, and your data is changing. And 2) If you have to update one value independent of the other, the syntax becomes needlessly verbose. Compare the tuple syntax:

my_pair = (new_first_value, my_pair[1])

To the list syntax:

my_pair[0] = new_first_value
Kevin
  • 74,910
  • 12
  • 133
  • 166