Re: Can tuples be replaced with lists all the time?
No, not always. You can use yuples as dictionary key as keys are immutable and you can't use it as list. -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
On 2014-02-23, Sam lightai...@gmail.com wrote: My understanding of Python tuples is that they are like immutable lists. If this is the cause, why can't we replace tuples with lists all the time (just don't reassign the lists)? Correct me if I am wrong. In addition to the things related to one being mutable and the other immutable, there is a certain tradition that often results in differnt usages for them. Though it's certainly not required, lists are more often used as variable length _homogeneous_ containers -- sort of like a variable length array or linked list in C. The type or meaning of element #N is the same as that of element #M. In constrast, tuples are often used as fixed-length heterogenous containers (more like a struct in C except the fields are named 0, 1, 2, 3, etc.). In a particular context, the Nth element of a tuple will always mean one thing (e.g. a person's last name) while the Mth element will always be something else (e.g. a person's age). -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
In article 64af70e3-6876-4fbf-8386-330d2f487...@googlegroups.com, Sam lightai...@gmail.com wrote: My understanding of Python tuples is that they are like immutable lists. If this is the cause, why can't we replace tuples with lists all the time (just don't reassign the lists)? Correct me if I am wrong. There are many tasks for which either a list or a tuple would work fine. But, not all. Basically: If you need something which is hashable (i.e. to be used as a dictionary key), you need to use a tuple. If you need something which you can mutate (say, to accumulate items as you go through a loop), then you need to use a list. Beyond that, you get into religious territory. -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
In article led9s7$req$1...@reader1.panix.com, Grant Edwards invalid@invalid.invalid wrote: In constrast, tuples are often used as fixed-length heterogenous containers (more like a struct in C except the fields are named 0, 1, 2, 3, etc.). In a particular context, the Nth element of a tuple will always mean one thing (e.g. a person's last name) while the Mth element will always be something else (e.g. a person's age). And, of course, namedtuples make that much more explicit. It also appears that tuples are more memory efficient. I just ran some quick tests on my OSX box. Creating a list of 10 million [1, 2, 3, 4, 5] lists gave me a 1445 MB process. The name number of (1, 2, 3, 4, 5) tuples was 748 MB. I'm sure this is implementation dependent, but it seems plausible to assume similar results will be had on other implementations. -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
On 23/02/2014 17:48, Roy Smith wrote: In article led9s7$req$1...@reader1.panix.com, Grant Edwards invalid@invalid.invalid wrote: In constrast, tuples are often used as fixed-length heterogenous containers (more like a struct in C except the fields are named 0, 1, 2, 3, etc.). In a particular context, the Nth element of a tuple will always mean one thing (e.g. a person's last name) while the Mth element will always be something else (e.g. a person's age). And, of course, namedtuples make that much more explicit. It also appears that tuples are more memory efficient. I just ran some quick tests on my OSX box. Creating a list of 10 million [1, 2, 3, 4, 5] lists gave me a 1445 MB process. The name number of (1, 2, 3, 4, 5) tuples was 748 MB. I'm sure this is implementation dependent, but it seems plausible to assume similar results will be had on other implementations. In CPython a list is overallocated so there's usually spare slots available if you want to add something to it. In contrast you know when you create the tuple just how big it is so no overallocation is needed. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
On 3/1/2014 4:20 PM, Mark Lawrence wrote: On 23/02/2014 17:48, Roy Smith wrote: It also appears that tuples are more memory efficient. I just ran some quick tests on my OSX box. Creating a list of 10 million [1, 2, 3, 4, 5] lists gave me a 1445 MB process. The name number of (1, 2, 3, 4, 5) tuples was 748 MB. I'm sure this is implementation dependent, but it seems plausible to assume similar results will be had on other implementations. The numbers sound right. In CPython a list is overallocated so there's usually spare slots available if you want to add something to it. In contrast you know when you create the tuple just how big it is so no overallocation is needed. Besides which, in CPython, a tuple is one block of memory, with a PyObject header followed by the object references, while a list uses 2 blocks of memory. The first has a PyObject header, followed by a reference to the list block, its allocated length (minimum 8 I believe), and its current in-use length. The over-allocated list block, which must also start on a 4 or 8 byte alignment boundary, has the object references plus extra space. -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
On Sunday, February 23, 2014 12:06:13 PM UTC+8, Sam wrote: My understanding of Python tuples is that they are like immutable lists. If this is the cause, why can't we replace tuples with lists all the time (just don't reassign the lists)? Correct me if I am wrong. == OK, lets be serious about high-level programming lnguages. Python is a dynamical typed ( name binding mechnism implicitly), imperative language with the built in auto GC and the heap plus stack managements in the bundled scriptor. A tuple is treated immutable and a list is mutable in Python. I suggest one can read the introductions about Erlang which is a non-imperative high level language in the 5 to 6 th gen programming languages in back-end server applictions for the robustness and maintainence costs. Neverthless, Python is regarded as a programming firendly language when comparing with other high level languages. -- https://mail.python.org/mailman/listinfo/python-list
Can tuples be replaced with lists all the time?
My understanding of Python tuples is that they are like immutable lists. If this is the cause, why can't we replace tuples with lists all the time (just don't reassign the lists)? Correct me if I am wrong. -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
Sam lightai...@gmail.com writes: My understanding of Python tuples is that they are like immutable lists. If this is the cause, why can't we replace tuples with lists all the time (just don't reassign the lists)? You can do that a lot of the time but not always. For example, you can use a tuple as a dictionary key, but not a list, since keys are supposed to be immutable. -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
On Sun, Feb 23, 2014 at 3:06 PM, Sam lightai...@gmail.com wrote: My understanding of Python tuples is that they are like immutable lists. If this is the cause, why can't we replace tuples with lists all the time (just don't reassign the lists)? Correct me if I am wrong. One reason is performance/efficiency. If Python knows this is never going to change, it can save some effort. But a more important reason is hashability. mapping = {} key = (1,2) mapping[key] = Hello key = (1,3) mapping[key] = World key = (2,3) mapping[key] = ! mapping {(1, 2): 'Hello', (1, 3): 'World', (2, 3): '!'} You can't do this with lists: key = [1,2] mapping[key] Traceback (most recent call last): File pyshell#12, line 1, in module mapping[key] TypeError: unhashable type: 'list' This is because any two tuples are either equal or not equal, they can't be otherwise. I can create another tuple and look up something in the above mapping: mapping[1,3] 'World' But with lists, their equality can change. lst1 = [1,2] lst2 = [1,3] lst1 == lst2 False lst1[1] += 1 lst1 == lst2 True lst1[1] += 1 lst1 == lst2 False So it would be very difficult and dangerous to try to use a list as a dictionary key. The only safe way to do it is by identity, and that's only useful in a very small set of situations. So Python happily declares that a tuple can be a dict key and a list can't, and that's now a key (if you'll excuse the pun) difference between them. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Can tuples be replaced with lists all the time?
Sam lightai...@gmail.com writes: My understanding of Python tuples is that they are like immutable lists. That's a common expression, but I think it's not a helpful way to think of them. Rather, the different sequence types have different semantic purposes: * For representing a sequence where each item means exactly the same no matter which position it's in (a “homogeneous sequence”), use a list. * For representing a sequence where the meaning of each item is strongly dependent on its position in the sequence (a “heterogeneous sequence”), use a tuple. See URL:http://docs.python.org/3/library/stdtypes.html for the official Python description of the type differences. If this is the cause, why can't we replace tuples with lists all the time (just don't reassign the lists)? Correct me if I am wrong. Because we need to represent different semantic concepts in our code, and have each type support the semantics with different operations. -- \ “I went camping and borrowed a circus tent by mistake. I didn't | `\ notice until I got it set up. People complained because they | _o__) couldn't see the lake.” —Steven Wright | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list