The only differences are that tuples are immutable and that lists have extra methods.

Is there ever a strong need for list-type data to be immutable? Evough to justify a whole extra data-type in the language?

Should they release a python 4 with it removed?

The only thing I can think of is as a default function parameter. This function is okay:

def dothings(a=(1,2)):
    print(a)
    a = (a[0], 3)

But this function misbehaves the second time it is called:

def dothings(a=[1,2]):
    print(a)
    a[1] = 3

But IMO the “mutable arguments” thing is another bug to be fixed in a hypothetical python 4. And even in python 3 you just write the function the recommended way, so there is not such a big problem.

def dothings(a=None):
    if a is None:
        a = [1, 2]
    print(a)
    a[1] = 3

The Python devs are clever guys though. There must be some really important reason to maintain both types?

  • SeerLite
    link
    23 years ago

    Lists should be homogeneous while tuples can be heterogeneous.

    Could you explain this bit, please? I’ve always understood that both can have both kinds of elements.

    Do you mean that in the usual contexts they’re used? Like: lists usually hold a variable number of things of the same type while tuples are sometimes expected to hold values of different kinds in a specific order. Something like that?

    • Tmpod
      link
      fedilink
      4
      edit-2
      3 years ago

      While lists can be heterogeneous (hold elements of differing types), their main purpose is not it, but rather to be a homogeneous ordered collections of elements. There are very little cases where you actually need a dynamic ordered collection with an heterogeneous set of elements.
      Tuples, on the other hand, provide a structure of heterogeneous data. They should, in fact, not be seen as “immutable” lists, even though they can act as such. So much so, as you have namedtuple which acts very akin to a C struct.

      Python is a very expresive language with a mostly clean syntax and nice data types as first-class citizens. This allows for code that clearly describes what you want. Sure, you could essentially use lists for every collection, and for structures, but that wouldn’t be clear at all.

      Edit: fixed fat fingered abomination lol