By Lucas Gabriel Sánchez


2009-03-09 15:41:25 8 Comments

What's the difference?

What are the advantages / disadvantages of tuples / lists?

18 comments

@Nikita 2013-09-19 11:07:28

Difference between list and tuple

  1. Literal

    someTuple = (1,2)
    someList  = [1,2] 
    
  2. Size

    a = tuple(range(1000))
    b = list(range(1000))
    
    a.__sizeof__() # 8024
    b.__sizeof__() # 9088
    

    Due to the smaller size of a tuple operation, it becomes a bit faster, but not that much to mention about until you have a huge number of elements.

  3. Permitted operations

    b    = [1,2]   
    b[0] = 3       # [3, 2]
    
    a    = (1,2)
    a[0] = 3       # Error
    

    That also means that you can't delete an element or sort a tuple. However, you could add new element to both list and tuple with the only difference that you will change id of the tuple by adding element

    a     = (1,2)
    b     = [1,2]  
    
    id(a)          # 140230916716520
    id(b)          # 748527696
    
    a   += (3,)    # (1, 2, 3)
    b   += [3]     # [1, 2, 3]
    
    id(a)          # 140230916878160
    id(b)          # 748527696
    
  4. Usage

    As a list is mutable, it can't be used as a key in a dictionary, whereas a tuple can be used.

    a    = (1,2)
    b    = [1,2] 
    
    c = {a: 1}     # OK
    c = {b: 1}     # Error
    
  5. Hashable Tuple is hashable while list is not. Hash functions return the hash value of the object, if it has one.

    Hashability of list and tuple.

@WanderingMind 2015-02-05 14:49:01

So what happens when I try to resize the list size to large values? Will it change the memory address (which I believe should change the id). Or will it throw me an error?

@Tom 2015-08-13 14:36:28

@WanderingMind: the memory address where the list values are stored is not the same as the memory address where the list object itself is stored.

@Cheng 2018-05-07 02:14:38

Why isn't sorting permitted on a tuple? Is it because it contains heterogeneous items?

@dmckee 2018-09-10 15:08:53

Hmmm ... all the code in this post except the first box under 3. Permitted operation shows the tuple case first. I know it is usual to show success then error, but that messed with my head for a few moments.

@mjkrause 2019-02-09 16:52:49

As shown under point 3, a single element list can be one_item_list = [a], but one_tuple = (a,) is the corresponding tuple. Note the comma following the variable name. But also note two_tuple = (a, b). This threw me off more than once (still there in Python 3).

@Daksh 2018-05-18 06:12:44

As people have already answered here that tuples are immutable while lists are mutable, but there is one important aspect of using tuples which we must remember

If the tuple contains a list or a dictionary inside it, those can be changed even if the tuple itself is immutable.

For example, let's assume we have a tuple which contains a list and a dictionary as

my_tuple = (10,20,30,[40,50],{ 'a' : 10})

we can change the contents of the list as

my_tuple[3][0] = 400
my_tuple[3][1] = 500

which makes new tuple looks like

(10, 20, 30, [400, 500], {'a': 10})

we can also change the dictionary inside tuple as

my_tuple[4]['a'] = 500

which will make the overall tuple looks like

(10, 20, 30, [400, 500], {'a': 500})

This happens because list and dictionary are the objects and these objects are not changing, but the contents its pointing to.

So the tuple remains immutable without any exception

@dmckee 2018-09-10 15:19:44

This post would be improved if you explained that "those can be changed even if the tuple itself in immutable." is because those object retain their identity while (so the tuple hasn't changed because it still contains the same objects...).

@iNet 2018-07-19 19:03:40

Difference between list and tuple

Tuples and lists are both seemingly similar sequence types in Python.

  1. Literal syntax

    We use parenthesis () to construct tuples and square brackets [ ] to get a new list. Also, we can use call of the appropriate type to get required structure — tuple or list.

    someTuple = (4,6)
    someList  = [2,6] 
    
  2. Mutability

    Tuples are immutable, while lists are mutable. This point is the base the for the following ones.

  3. Memory usage

    Due to mutability, you need more memory for lists and less memory for tuples.

  4. Extending

    You can add a new element to both tuples and lists with the only difference that the id of the tuple will be changed (i.e., we’ll have a new object).

  5. Hashing

    Tuples are hashable and lists are not. It means that you can use a tuple as a key in a dictionary. The list can't be used as a key in a dictionary, whereas a tuple can be used

    tup      = (1,2)
    list_    = [1,2] 
    
    c = {tup   : 1}     # ok
    c = {list_ : 1}     # error
    
  6. Semantics

    This point is more about best practice. You should use tuples as heterogeneous data structures, while lists are homogenous sequences.

@dmckee 2018-09-10 15:17:23

My vote here for the explicit discussion of extension and hashing, which I didn't notice in the otherwise very good existing answers.

@Muhammad Ibrahim Mian 2018-06-20 22:11:19

The basic difference that I have found is that lists are mutable where as tuples are immutable. Tuples are good for computations in which they need not be changed.

@odradek 2018-06-20 22:17:23

What dos this add over the accepted answer?

@dan-gph 2009-03-09 17:14:47

If you went for a walk, you could note your coordinates at any instant in an (x,y) tuple.

If you wanted to record your journey, you could append your location every few seconds to a list.

But you couldn't do it the other way around.

@RayLuo 2013-03-29 15:47:05

This example looks like merely a convention. People could argue "I can still use [x, y] to note coordinates if I want". Therefore, this answer is considered uncompleted unless one more sentence: "Read @nikow 's post for why you should not use list to store coordinates"

@dan-gph 2013-04-14 00:00:17

@Iceberg, my answer is meant to help develop intuition. It's not meant to explore every nuance of the topic.

@Hao Wang 2015-01-04 06:54:32

Nice example +1. It emphasizes the complementary nature of the tuple elements (here the coordinates), which is the reason why modifying any one of them is not allowed -- because it changes the meaning of the whole tuple (here the position of one point).

@Amit Upadhyay 2017-02-12 17:09:28

that's what the mathematics says..

@nikow 2009-03-09 16:02:51

Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.

Using this distinction makes code more explicit and understandable.

One example would be pairs of page and line number to reference locations in a book, e.g.:

my_location = (42, 11)  # page number, line number

You can then use this as a key in a dictionary to store notes on locations. A list on the other hand could be used to store multiple locations. Naturally one might want to add or remove locations from the list, so it makes sense that lists are mutable. On the other hand it doesn't make sense to add or remove items from an existing location - hence tuples are immutable.

There might be situations where you want to change items within an existing location tuple, for example when iterating through the lines of a page. But tuple immutability forces you to create a new location tuple for each new value. This seems inconvenient on the face of it, but using immutable data like this is a cornerstone of value types and functional programming techniques, which can have substantial advantages.

There are some interesting articles on this issue, e.g. "Python Tuples are Not Just Constant Lists" or "Understanding tuples vs. lists in Python". The official Python documentation also mentions this

"Tuples are immutable, and usually contain an heterogeneous sequence ...".

In a statically typed language like Haskell the values in a tuple generally have different types and the length of the tuple must be fixed. In a list the values all have the same type and the length is not fixed. So the difference is very obvious.

Finally there is the namedtuple in Python, which makes sense because a tuple is already supposed to have structure. This underlines the idea that tuples are a light-weight alternative to classes and instances.

@Baltimark 2009-03-09 17:31:07

+1, especially for your second link with a great example. Love this quote: "This tuple functions as a lightweight record or struct."

@Matthew Cornell 2012-09-04 14:41:33

"lists are homogeneous sequences" - I'm new to Python, but aren't lists heterogeneous? From docs.python.org/py3k/tutorial/introduction.html : "List items need not all have the same type." But maybe you're speaking about the formal concept, and not the Python take on it.

@Kos 2013-06-26 12:29:43

1. Heterogenous lists are OK - what's usually needed is some homogenous interface among the elements. 2. Using a tuple in place of an immutable list is a form of micro-optimisation.

@kindall 2013-10-28 18:58:23

A good semantic synonym for "tuple" might be "record." It's a collection of related data items in a specific sequence. In fact I feel like collections.namedtuple would be better called collections.record. It would make no sense to swap, say, the name and address in a customer record; in fact, doing so would generally be an error, which the tuple's immutability prevents you from committing.

@kindall 2014-01-26 22:05:59

In fact I just remembered that the ancient DIF file format uses the term "tuple" to refer to a row, or record, of data.

@naught101 2014-03-19 03:41:05

So I just read the James Tauber blog post, and immediately thought "ok, then what's the difference between namedtuple and a dictionary?" Just immutability, I suppose, since there's no longer any real semantic difference....

@Trilarion 2014-03-19 09:13:40

You could probably still use a list everywhere where tuples are used without major problems. So one could see tuples as specialization of lists and that's how I think they should be seen. It's not really about structure or order. Tuples are just immutable lists of length two.

@nikow 2014-03-20 15:58:38

@naught101: No, not really. Namedtuples are more like light-weight classes, they don't have much in common with dicts.

@nikow 2014-03-20 16:05:52

@Triliarion: Of course you can ignore the semantic distinction without causing any direct technical problem. But I think my answer provides good reasons why you should not do that.

@naught101 2014-03-21 00:53:21

@Trilarion: tuples can be of any length...

@abarnert 2014-11-26 20:38:21

The "just" part of "tuples are not just immutable lists" is important, because, along with being usable as static-shaped heterogeneous records, they're also usable as homogenous sequences that are just like lists except immutable—so, e.g., you can use them as a set element or dict key (or as a multi-dimensional array index, for that matter). Your answer is completely right that usually you use a tuple because you want a light-weight record, it's just that "usually" isn't "always".

@abarnert 2014-11-26 20:39:50

@naught101: The main difference between a namedtuple and a dictionary is that the namedtuple has attributes, with static names that are part of the source code, while a dictionary has members, with dynamic names that are part of your data. (Of course you can fudge this difference by using getattr with data/indexing with literals, and occasionally you have to, but usually you don't want to.)

@nikow 2014-11-27 14:25:55

@abarnert: True, that is an important point (and I'm not arguing against this). To some extend this fits the semantic distinction, because there are probably more situations where you would use structured tuples as dict keys or set values than lists (e.g., using a book location as a dict key makes sense, using a list of locations as a key is less common). But of course there are legitimate counterexamples (e.g., polynomials encoded in the standard way like [3,0,0,1]).

@abarnert 2014-12-01 20:18:30

@nikow: Thanks for coming up with a good example for my point. :) Anyway, I agree with you that there are probably more situations where you want a set of structured tuples than sequences, I just wanted to point out that it's not 100%, because it's the only minor issue your answer doesn't already cover perfectly.

@Sebastian Mach 2014-12-04 09:15:52

@nikow: Regarding What would you do with such a list?, I always shiver when ppl use lack of fantasy as an argument. Using mixed type lists works great e.g. for some hierarchical data structures, where each list is composed of child-lists and value-elements.

@nikow 2014-12-04 16:02:19

@phresnel: In your hierarchical data structure example I would not consider the list to be heterogenous. In the standard composite pattern both leaves and composites are subclasses of a component base class - so you have a homogenous list of components and use polymorphism.

@Sebastian Mach 2014-12-04 16:09:16

@nikow: Except when using polymorphism makes the code less readable. In the general case, I agree. But in small, very local pieces of code, a list of (element, list) is often worth a thousand classes.

@abarnert 2014-12-05 18:39:32

@phresnel: You realize that what you just wrote is a tuple, not a list, and therefore you're actually making nikow's point here? (That's not to say that you can't come up with examples where using a list as a mutable tuple makes sense, just like the examples I gave of using a tuple as an immutable list so you can stick it in a set, just that there are default intended semantics; that's why the first thing you think of when you want a heterogenous pair is a tuple.

@Celeritas 2015-08-07 23:24:50

Isn't it misleading to say tuples are heterogeneous and lists are homogeneous? For example a list can have mixtures of different data types i.e. l = [1, 2, 'a']. I don't understand what you're talking about.

@Akshay Pardhanani 2016-06-22 11:03:20

Drawing a parallel to a language like 'C' would it be correct to say that tuples are similar to structs whereas lists are similar to arrays?

@nikow 2016-06-23 15:50:47

@Akshay: Yes, exactly (especially namedtuples).

@metal 2018-02-21 19:23:00

@Celeritas It is misleading in a strict sense because the language does not enforce homogeneity in lists (Python is for consenting adults, after all). The point is more about writing code within the conventions of the community. In such a context, the tuples and lists have different semantic meanings that can clarify what the code is meant to do.

@dmckee 2018-09-10 15:13:52

The key here is that 'heterogeneous" and "homogeneous" in this post are referring to semantic kind not programmatic type. This is quite natural in a object-oriented context using IsA inheritance, but it also make sense in @Sebastian's tree example as long as we understand that the list contents are subtrees which may be (implicitly) empty, leafs, or branches. (The tree case is also exactly how lisp is structured.)

@Albus Dumbledore 2017-09-26 16:12:11

A direction quotation from the documentation on 5.3. Tuples and Sequences:

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

@Rahul Sawriya 2017-06-21 05:35:59

Lists are mutable and tuples are immutable. Just consider this example.

a = ["1", "2", "ra", "sa"]    #list
b = ("1", "2", "ra", "sa")    #tuple

Now change index values of list and tuple.

a[2] = 1000
print a     #output : ['1', '2', 1000, 'sa']
b[2] = 1000
print b     #output : TypeError: 'tuple' object does not support item assignment.

Hence proved the following code is invalid with tuple, because we attempted to update a tuple, which is not allowed.

@Shep 2013-08-12 07:06:24

It's been mentioned that the difference is largely semantic: people expect a tuple and list to represent different information. But this goes further than a guideline; some libraries actually behave differently based on what they are passed. Take NumPy for example (copied from another post where I ask for more examples):

>>> import numpy as np
>>> a = np.arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> idx = (1,1)
>>> a[idx]
4
>>> idx = [1,1]
>>> a[idx]
array([[3, 4, 5],
       [3, 4, 5]])

The point is, while NumPy may not be part of the standard library, it's a major Python library, and within NumPy lists and tuples are completely different things.

@Eric 2013-09-19 13:14:08

This isn't really a helpful answer. The difference is that type(a_list) != type(a_tuple), so any piece of library code branching based on type(x) will behave differently

@Shep 2013-09-20 07:56:34

good point, I've edited the post: this is really just pointing out that the semantic guidelines are hardcoded into some libraries.

@abarnert 2014-12-01 20:23:24

It might be better to use examples from the stdlib/builtins than from third-party libraries. There are a number of places where you can use a single value or a tuple of values, and a list or other sequence is considered a single value. For example, '%d %d' % [2, 3] is a TypeError, because you're trying to pass a list to the first %d and you're not passing any value to the second %d. (However, there are counter-examples to this too, like max…)

@Shep 2014-12-02 21:22:08

that's interesting, I didn't know there were any examples of this in the python standard library. A number of places you say?

@Xinyang Li 2016-04-08 09:10:36

First of all, they both are the non-scalar objects (also known as a compound objects) in Python.

  • Tuples, ordered sequence of elements (which can contain any object with no aliasing issue)
    • Immutable (tuple, int, float, str)
    • Concatenation using + (brand new tuple will be created of course)
    • Indexing
    • Slicing
    • Singleton (3,) # -> (3) instead of (3) # -> 3
  • List (Array in other languages), ordered sequence of values
    • Mutable
    • Singleton [3]
    • Cloning new_array = origin_array[:]
    • List comprehension [x**2 for x in range(1,7)] gives you [1,4,9,16,25,36] (Not readable)

Using list may also cause an aliasing bug (two distinct paths pointing to the same object).

@Afflatus 2016-03-22 13:48:58

This is an example of Python lists:

my_list = [0,1,2,3,4]
top_rock_list = ["Bohemian Rhapsody","Kashmir","Sweet Emotion", "Fortunate Son"]

This is an example of Python tuple:

my_tuple = (a,b,c,d,e)
celebrity_tuple = ("John", "Wayne", 90210, "Actor", "Male", "Dead")

Python lists and tuples are similar in that they both are ordered collections of values. Besides the shallow difference that lists are created using brackets "[ ... , ... ]" and tuples using parentheses "( ... , ... )", the core technical "hard coded in Python syntax" difference between them is that the elements of a particular tuple are immutable whereas lists are mutable (...so only tuples are hashable and can be used as dictionary/hash keys!). This gives rise to differences in how they can or can't be used (enforced a priori by syntax) and differences in how people choose to use them (encouraged as 'best practices,' a posteriori, this is what smart programers do). The main difference a posteriori in differentiating when tuples are used versus when lists are used lies in what meaning people give to the order of elements.

For tuples, 'order' signifies nothing more than just a specific 'structure' for holding information. What values are found in the first field can easily be switched into the second field as each provides values across two different dimensions or scales. They provide answers to different types of questions and are typically of the form: for a given object/subject, what are its attributes? The object/subject stays constant, the attributes differ.

For lists, 'order' signifies a sequence or a directionality. The second element MUST come after the first element because it's positioned in the 2nd place based on a particular and common scale or dimension. The elements are taken as a whole and mostly provide answers to a single question typically of the form, for a given attribute, how do these objects/subjects compare? The attribute stays constant, the object/subject differs.

There are countless examples of people in popular culture and programmers who don't conform to these differences and there are countless people who might use a salad fork for their main course. At the end of the day, it's fine and both can usually get the job done.

To summarize some of the finer details

Similarities:

  1. Duplicates - Both tuples and lists allow for duplicates
  2. Indexing, Selecting, & Slicing - Both tuples and lists index using integer values found within brackets. So, if you want the first 3 values of a given list or tuple, the syntax would be the same:

    >>> my_list[0:3]
    [0,1,2]
    >>> my_tuple[0:3]
    [a,b,c]
    
  3. Comparing & Sorting - Two tuples or two lists are both compared by their first element, and if there is a tie, then by the second element, and so on. No further attention is paid to subsequent elements after earlier elements show a difference.

    >>> [0,2,0,0,0,0]>[0,0,0,0,0,500]
    True
    >>> (0,2,0,0,0,0)>(0,0,0,0,0,500)
    True
    

Differences: - A priori, by definition

  1. Syntax - Lists use [], tuples use ()

  2. Mutability - Elements in a given list are mutable, elements in a given tuple are NOT mutable.

    # Lists are mutable:
    >>> top_rock_list
    ['Bohemian Rhapsody', 'Kashmir', 'Sweet Emotion', 'Fortunate Son']
    >>> top_rock_list[1]
    'Kashmir'
    >>> top_rock_list[1] = "Stairway to Heaven"
    >>> top_rock_list
    ['Bohemian Rhapsody', 'Stairway to Heaven', 'Sweet Emotion', 'Fortunate Son']
    
    # Tuples are NOT mutable:       
    >>> celebrity_tuple
    ('John', 'Wayne', 90210, 'Actor', 'Male', 'Dead')
    >>> celebrity_tuple[5]
    'Dead'
    >>> celebrity_tuple[5]="Alive"
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'tuple' object does not support item assignment
    
  3. Hashtables (Dictionaries) - As hashtables (dictionaries) require that its keys are hashable and therefore immutable, only tuples can act as dictionary keys, not lists.

    #Lists CAN'T act as keys for hashtables(dictionaries)
    >>> my_dict = {[a,b,c]:"some value"}
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'list'
    
    #Tuples CAN act as keys for hashtables(dictionaries)
    >>> my_dict = {("John","Wayne"): 90210}
    >>> my_dict
    {('John', 'Wayne'): 90210}
    

Differences - A posteriori, in usage

  1. Homo vs. Heterogeneity of Elements - Generally list objects are homogenous and tuple objects are heterogeneous. That is, lists are used for objects/subjects of the same type (like all presidential candidates, or all songs, or all runners) whereas although it's not forced by), whereas tuples are more for heterogenous objects.

  2. Looping vs. Structures - Although both allow for looping (for x in my_list...), it only really makes sense to do it for a list. Tuples are more appropriate for structuring and presenting information (%s %s residing in %s is an %s and presently %s % ("John","Wayne",90210, "Actor","Dead"))

@Dave X 2018-03-28 20:13:19

I like the hashtable/hashable example for explaining a reason for immutability-- you can use a tuple(record/struct/coordinate/vector/point) as a complex key into a dict.

@Antti Haapala 2016-09-22 16:13:04

The PEP 484 -- Type Hints says that the types of elements of a tuple can be individually typed; so that you can say Tuple[str, int, float]; but a list, with List typing class can take only one type parameter: List[str], which hints that the difference of the 2 really is that the former is heterogeneous, whereas the latter intrinsically homogeneous.

Also, the standard library mostly uses the tuple as a return value from such standard functions where the C would return a struct.

@Dave Webb 2009-03-09 15:42:38

The key difference is that tuples are immutable. This means that you cannot change the values in a tuple once you have created it.

So if you're going to need to change the values use a List.

Benefits to tuples:

  1. Slight performance improvement.
  2. As a tuple is immutable it can be used as a key in a dictionary.
  3. If you can't change it neither can anyone else, which is to say you don't need to worry about any API functions etc. changing your tuple without being asked.

@abarnert 2014-11-26 20:45:18

Note that a tuple is only immutable if all of its elements are. You could say the same is true of all immutable collections, like frozenset or the various third-party frozen dict/tree/etc. types, but none of those allow you to add mutable elements. (And of course a tuple is only hashable if all of its elements are, which is handles in the usual EAFP way, so d[1, [2]] will raise TypeError: unhashable type: 'list'.)

@Theoremiser 2017-03-26 10:49:31

A tuple can only be used as a key in a dictionary if all of its elements are immutable. See here

@Tony 2014-09-30 19:01:37

List is mutable and tuples is immutable. The main difference between mutable and immutable is memory usage when you are trying to append an item.

When you create a variable, some fixed memory is assigned to the variable. If it is a list, more memory is assigned than actually used. E.g. if current memory assignment is 100 bytes, when you want to append the 101th byte, maybe another 100 bytes will be assigned (in total 200 bytes in this case).

However, if you know that you are not frequently add new elements, then you should use tuples. Tuples assigns exactly size of the memory needed, and hence saves memory, especially when you use large blocks of memory.

@Roger Fan 2014-09-30 19:25:10

While some of that is technically true, that isn't really the crucial difference between mutable and immutable types. The bigger difference is that mutable types can be changed after construction, while immutable types can't.

@Roger Fan 2014-09-30 19:32:55

That's not the why either. Memory and mutability have nothing to do with each other. That's simply an implementation detail specific to lists. Memory is also not assigned to variables, it's assigned to the objects. Variables are then just references to those objects.

@duffymo 2009-03-09 15:41:59

Lists are mutable; tuples are not.

From docs.python.org/2/tutorial/datastructures.html

Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

@nikow 2009-03-09 16:07:29

I really think you should also consider the semantic implications (see my answer below).

@duffymo 2009-03-09 16:43:49

D'oh! I'll try to do better next time...

@S.Lott 2009-03-09 18:33:51

Feel free to edit your answer to include a relevant link.

@duffymo 2009-03-09 19:50:02

Hardly seems worth the effort now, but thanks for the heads up.

@duffymo 2009-09-01 22:09:39

Six months later, and this is all you can contribute? I gave that answer at the time because in the four hours that passed between my original answer and the comment others had more than made up the difference. Was the answer incorrect? Not really, although it was improved upon by nikow. So what would a link really add?

@duffymo 2012-01-01 00:11:11

Almost four years after the fact, and this answer deserves a downvote? Ridiculous.

@Sebastian Mach 2012-07-19 12:46:40

It is funny to see how other answers here have the same quality like this, but only this gets hated because of one commenter stating "-1". And there is another answer that has just 0.5 times the quality, but only 0.1 times the downvotes.

@Alexey Kuzminich 2013-04-26 01:07:47

@Alexey Kuzminich 2013-04-26 01:12:56

And here's the quote from docs.python.org/2/tutorial/datastructures.html Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

@Alexey Kuzminich 2013-04-26 01:19:41

@duffymo: Doesn't matter how old this question - why not provide a link or quote

@duffymo 2013-04-26 01:28:57

Honestly, it adds little or no value beyond what's already here. Let's see: I wrote "Lists are mutable; tuples are not." You provided a quote that says "Tuples are immutable...". That's HUGE!

@ArtOfWarfare 2013-07-27 04:01:47

@AlexeyKuzminich - so edit the answer to incorporate them? Or is your rep not high enough to edit other people's answers?

@antred 2015-10-20 15:15:02

@duffymo I think this answer is the clearest and most concise on this page. It names the sole really important difference between tuples and lists and doesn't babble on endlessly about this blatantly false homogeneous-vs-heterogeneous hogwash.

@user2436485 2013-06-06 11:14:54

The values of list can be changed any time but the values of tuples can't be change.

The advantages and disadvantages depends upon the use. If you have such a data which you never want to change then you should have to use tuple, otherwise list is the best option.

@Srinivas Reddy Thatiparthy 2013-04-19 05:43:14

Lists are for looping, tuples are for structures i.e. "%s %s" %tuple.

Lists are usually homogeneous, tuples are usually heterogeneous.

Lists are for variable length, tuples are for fixed length.

@pquers 2009-03-09 20:29:55

Lists are intended to be homogeneous sequences, while tuples are heterogeneous data structures.

@Jonathon Reinhart 2013-11-25 02:14:34

At this point, this answer doesn't add anything to the discussion, as there are many other better answers.

Related Questions

Sponsored Content

42 Answered Questions

[SOLVED] What does the "yield" keyword do?

27 Answered Questions

[SOLVED] Difference between append vs. extend list methods in Python

29 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3150785 View
  • 2595 Score
  • 29 Answer
  • Tags:   python list

20 Answered Questions

[SOLVED] How to clone or copy a list?

35 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2111208 View
  • 3152 Score
  • 35 Answer
  • Tags:   python list

23 Answered Questions

[SOLVED] What is the difference between @staticmethod and @classmethod?

57 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

38 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

21 Answered Questions

[SOLVED] Difference between __str__ and __repr__?

10 Answered Questions

[SOLVED] What are "named tuples" in Python?

Sponsored Content