List vs Tuples

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2006
Posts: 242
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

List vs Tuples

 
0
  #1
Aug 13th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 23
Reputation: Zetlin is an unknown quantity at this point 
Solved Threads: 1
Zetlin's Avatar
Zetlin Zetlin is offline Offline
Newbie Poster

Re: List vs Tuples

 
2
  #2
Aug 13th, 2008
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.

  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.

  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.

  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
Why is it that no human being knows the true meaning of life? For the reason that those who have found it are too caught up in their rage to understand that it ends.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: List vs Tuples

 
0
  #3
Aug 13th, 2008
In addition to Zetlin's nice explanation, tuples are used in passing arguments to and from functions ...
  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 ...
  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
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 399
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

Re: List vs Tuples

 
0
  #4
Aug 25th, 2008
nothing against zetlins first reply but he did just copy that from the tutorial from here
don't judge me because I'm a year 8!

'it is better to fight for something than to live for nothing'General George S Patton
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: List vs Tuples

 
0
  #5
Aug 25th, 2008
Originally Posted by leegeorg07 View Post
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:
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
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC