This is a discussion on What is the difference between tuple and list? within the Python forums, part of the Software Development category; What is the difference between tuple and list?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi Vadi, Lists and tuples are both sequences, and can be used interchangeably in many situations. The big difference is that lists are mutable (can be modified), and tuples are immutable (cannot be modified). Lists also have a richer API (for example the index and count methods). If you are going to need to change the contents of the sequence (appending, removing, altering a value, etc), then use a list. If you require the sequence to be immutable (such as being used as the key in a dictionary), then use a tuple. In general, tuples should be a little more efficient than lists. However, altering a list is more efficient than creating a new tuple, so "always prefer tuples" does not necessarily lead to a faster overall program. Unless performance is critical, I wouldn't worry about this factor. IMHO, a more important reason to prefer tuples is that since they are immutable, you don't have to worry about side effects. If you call a function and pass it a list, you have no guarantee that the list won't be changed during the function call. With tuples, any attempt to change the tuple will raise an exception. If it is important to the caller that the sequence remain unchanged, then a tuple is a safer representation for that sequence. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| List out all the similarities/difference between an Abstract class and Interface? | H2o | ASP and ASP.NET Programming | 1 | 07-24-2007 03:16 AM |
| List Difference between client server testing and web testing | itbarota | Software Testing | 1 | 07-23-2007 11:26 PM |
| list box | nssukumar | Flash Actionscript Programming | 0 | 07-10-2007 06:34 AM |
| List in Google in Just 7 days | vadivelanvaidyanathan | 3 | 04-26-2007 01:21 PM | |
| Does anyone else have a Life To Do List? | cocowgirl29 | The Lounge | 5 | 04-05-2007 06:58 AM |