what is difference between list and tuple?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthika aru
    New Member
    • Mar 2011
    • 1

    what is difference between list and tuple?

    please explain with some example.
    Tell some important characteristics also.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    A list (notice the square brackets):
    Code:
    >>> alist = [1,2,3,4]
    >>> dir(alist)
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
    A tuple (notice the parentheses):
    Code:
    >>> atuple = (1,2,3)
    >>> dir(atuple)
    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__str__']
    The biggest difference is a list is mutable and a tuple is immutable. A tuple can be used as a dictionary key and a list cannot. They also have different methods.

    Comment

    Working...