943,902 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 6984
  • Python RSS
Aug 13th, 2008
0

List vs Tuples

Expand Post »
Hello all,

Can any one explain me whats the main different between them two data structures. I have been like working on them quite lot now. When do we use tuples and when do we use list. Any specific explains are much appreciated.

Thanks a lot

ssharish
Similar Threads
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Aug 13th, 2008
2

Re: List vs Tuples

Lists are what they seem - a list of values. Each one of them is numbered, starting from zero - the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end. Example: Your many cats' names.

Python Syntax (Toggle Plain Text)
  1. "A list"
  2.  
  3. cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']
  4.  
  5. "If you want to print for example the first name on the list you would do the following"
  6.  
  7. print cats[0]
  8.  
  9. "Also if you want to add a name to the list you would do this"
  10.  
  11. cats.append('Catherine')
  12.  
  13. "To remove a name you would do this"
  14.  
  15. del cats[0]

Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference. Example: the names of the months of the year.

Python Syntax (Toggle Plain Text)
  1. "Here is an example of a tuple"
  2.  
  3. months = ('January','February','March','April','May','June',\
  4. 'July','August','September','October','November',' December')
  5.  
  6. "So that’s the difference between lists and tuples. A list can be modified but a tuple cannot be modified in anyway(unless changed in the source code of the program)"

There are also dictionaries (I know you did not ask about them but I am going to tell you about them anyway).

Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.

Python Syntax (Toggle Plain Text)
  1. "Here is a simple phonebook"
  2.  
  3. phonebook = {'Andrew Parson':8806336, \
  4. 'Emily Everett':6784346, 'Peter Power':7658344, \
  5. 'Lewis Lame':1122345}
  6.  
  7. "To add a name to the phonebook you would do the following"
  8.  
  9. phonebook['Gingerbread Man'] = 1234567
  10.  
  11. "If you want to remove a name you would do the same as you would do in a list"
  12.  
  13. del phonebook['Andrew Parson']

So there you go hopefully that cleared things a bit for you and if you want to know more about the subject check out this website where the subject is explain more in depth.

http://docs.python.org/tut/node7.html
Reputation Points: 22
Solved Threads: 10
Junior Poster in Training
Zetlin is offline Offline
66 posts
since Jul 2008
Aug 13th, 2008
0

Re: List vs Tuples

In addition to Zetlin's nice explanation, tuples are used in passing arguments to and from functions ...
Python Syntax (Toggle Plain Text)
  1. def myfunction(a, b, c):
  2. d = 2 * a
  3. e = b + 5
  4. f = c - 1
  5. return d, e, f
  6.  
  7. a = 1
  8. b = 2
  9. c = 3
  10.  
  11. mytuple = myfunction(a, b, c)
  12. d, e, f = myfunction(a, b, c)
  13.  
  14. print a, b, c # 1 2 3
  15. print mytuple # (2, 7, 2)
  16. print d, e, f # 2 7 2
Then there is the tuple swap that allows you to avoid a temporary variable ...
python Syntax (Toggle Plain Text)
  1. x = 77
  2. y = 99
  3. print x, y # 77 99
  4.  
  5. # do a tuple swap
  6. y, x = x, y
  7. print x, y # 99 77
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Aug 25th, 2008
0

Re: List vs Tuples

nothing against zetlins first reply but he did just copy that from the tutorial from here
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008
Aug 25th, 2008
0

Re: List vs Tuples

Click to Expand / Collapse  Quote originally posted by leegeorg07 ...
nothing against zetlins first reply but he did just copy that from the tutorial from here
He should have given some credit to the original source.

Reminds me of a quote from Albert Einstein:
Quote ...
Plagiarism is stealing from one source.
Research is stealing from many sources
.
Last edited by sneekula; Aug 25th, 2008 at 12:54 pm. Reason: albert
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: convert to xml
Next Thread in Python Forum Timeline: py2exe for Linux (still to create exe files for Windows)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC