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

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.

"A list"

cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']

"If you want to print for example the first name on the list you would do the following"

print cats[0]

"Also if you want to add a name to the list you would do this"

cats.append('Catherine')

"To remove a name you would do this"

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.

"Here is an example of a tuple"

months = ('January','February','March','April','May','June',\
'July','August','September','October','November','  December')

"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.

"Here is a simple phonebook"

phonebook = {'Andrew Parson':8806336, \
'Emily Everett':6784346, 'Peter Power':7658344, \
'Lewis Lame':1122345}

"To add a name to the phonebook you would do the following"

phonebook['Gingerbread Man'] = 1234567

"If you want to remove a name you would do the same as you would do in a list"

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

commented: Nice explanation! Welcome to the Forum! +9
commented: Nice explanation +2

In addition to Zetlin's nice explanation, tuples are used in passing arguments to and from functions ...

def myfunction(a, b, c):
    d = 2 * a
    e = b + 5
    f = c - 1
    return d, e, f

a = 1
b = 2
c = 3

mytuple = myfunction(a, b, c)
d, e, f = myfunction(a, b, c)

print a, b, c  # 1 2 3
print mytuple  # (2, 7, 2)
print d, e, f  # 2 7 2

Then there is the tuple swap that allows you to avoid a temporary variable ...

x = 77
y = 99
print x, y  # 77 99

# do a tuple swap
y, x = x, y
print x, y  # 99 77
Member Avatar for leegeorg07

nothing against zetlins first reply but he did just copy that from the tutorial from here

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
.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.