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.
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.