By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,767 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,767 IT Pros & Developers. It's quick & easy.

what is difference between list and tuple?

P: 1
please explain with some example.
Tell some important characteristics also.
Mar 21 '11 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
A list (notice the square brackets):
Expand|Select|Wrap|Line Numbers
  1. >>> alist = [1,2,3,4]
  2. >>> dir(alist)
  3. ['__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):
Expand|Select|Wrap|Line Numbers
  1. >>> atuple = (1,2,3)
  2. >>> dir(atuple)
  3. ['__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.
Mar 22 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.