3

Say I have components that I need to iterate through, for example "From" and "To." As a convenience, I can iterate through these strings, but I don't need them stored for any extended period since I only have to do this once. Would it be better to use a list or tuple in this instance?

info = {}
for type in ('from', 'to'):
   info[type] = fetch(type);

Note that the above will work exactly the same if the tuple were changed to a list; I'm wondering if there is some preference.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 1
    this might be informative: http://stackoverflow.com/questions/68630/are-tuples-more-efficient-than-lists-in-python – Hamish Mar 26 '12 at 22:05

5 Answers5

8

Tuples are most likely more performant as they are immutable. The disassembly also shows that less opcodes are used with a tuple:

  2           0 SETUP_LOOP              14 (to 17)
              3 LOAD_CONST               3 (('a', 'b'))
              6 GET_ITER

vs

  2           0 SETUP_LOOP              20 (to 23)
              3 LOAD_CONST               1 ('a')
              6 LOAD_CONST               2 ('b')
              9 BUILD_LIST               2
             12 GET_ITER

It doesn't really matter though, if that bit of performance matters, you should write C or assembly code instead.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
7

If you're not going to do changes to it, I think a tuple applies better. I only use lists when I need to do dynamic operations to the collection such as append, subtract etc.

Plus, in most situations accessing a tuple is faster. That being said you should always test your specific use case before deciding.

vascop
  • 4,972
  • 4
  • 37
  • 50
1

Tuples are apparently smaller in memory and faster to create but with 1 or 2 items I can't imagine how it would matter in 99.99% of situations.

SpliFF
  • 38,186
  • 16
  • 91
  • 120
1

Tuples are probably better than lists in this case, but you can also do

for item in a, b:
    process(item)

I am wondering what is really happening in this case :P

Edit: It translates into:

3 LOAD_CONST               3 (('a', 'b'))

which seems to be a tuple...

tchap
  • 1,047
  • 8
  • 10
1

If I understand your question correctly, you are asking what is most idiomatic to use in this situation. (Performance and memory-efficiency have virtually no importance here, since you said you are only doing this once.)

The answer is that for most of Python's existence, people tended to use tuples for throwaway constants to iterate over, mainly because of the habit to use something immutable when there is no intention of mutating it.

[Edit: Got rid of some irrelevant stuff about sets. I was too eager to shoehorn it into the answer when it didn't actually contribute.]

Note that there is a sometimes vocal minority that insists on list. The rationale for this is that lists were originally conceived as the "array" type for Python, while tuples were the "record" type. That is, lists were meant for holding multiple objects of a single type (like a collection of test scores) whereas tuples were meant for holding arbitrary related objects (like name, address, and phone number). Strict adherence to this is kind of archaic and quaint now, though.

John Y
  • 14,123
  • 2
  • 48
  • 72
  • You are completely correct in your understanding of my question. Why would *set literal* syntax be preferred over tuple in this instance (I think it's uglier too)? – Explosion Pills Mar 26 '12 at 23:56
  • 1
    @tandu: Sorry, it actually wouldn't in this instance. I've edited that material out of my answer because indeed, it neither makes much sense nor is common practice. (What people *do* like to use set literals for, that they traditionally used tuples for, is to test membership in a small, constant collection of values.) – John Y Mar 27 '12 at 02:07