this post was submitted on 29 Sep 2021
7 points (88.9% liked)

Python

3186 readers
2 users here now

News and discussions about the programming language Python


founded 5 years ago
MODERATORS
 

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?

you are viewing a single comment's thread
view the rest of the comments
[–] birokop@lemmy.ml 3 points 3 years ago (1 children)

Don't really know python but i think your lists are quite performance heavy because of all the features, while tuples are closer to arrays in a language like java, and are simple memory blocks that can be worked with much faster. Don't take my word for it though :P

[–] roastpotatothief@lemmy.ml 0 points 3 years ago* (last edited 3 years ago) (1 children)

This could be the right answer. I know tuples use slightly less memory. But I'm not if that's so important. I don't think programs ever need to iterate over 1 million tuple entries, where speed or memory would be important.

[–] ksynwa@lemmy.ml 0 points 3 years ago

One important reason why tuples exist is that unlike lists they are immutable. So they are hashable and can be used as keys in dictionaries among other things.