Tuple List
Why is there a tuple and list in Python. They seemed to perform similar things.
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
The tuple and the list have a few things in common. They are both indexed containers for objects. The tuple is a very simple container to put some objects together, separated by commas, so you can send them to and from functions or stick them into lists as a group wrapped in ().
The list is a much more versatile beast, it is actually a doubly linked list, this way you can easily remove or insert objects (call them elements if you like), it is also indexed like an array. You can sort it, pop it, join it, search it, slice it and dice it, even comprehend it!
The tuple is simple and consumes little memory, the list is, by its nature, much more memory hungry. At first glance they look alike, but there is no comparison, each has their role to play.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Now it all makes more sense, the two have each a very much definite purpose. In your tutorial on integrs you talk about pointers to memory, have tuples then just pointers to objects?
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
You can call them pointers to the address of each object. You can find the address with id(tuple_element). Take a look at this ...
[php]# globals() brings up the present dictionary of Python internals
# this would make a, b, c reference (point to) the same integer object
a = b = c = 7
tuple1 = (a, b, c)
for member in tuple1:
print member, id(member) # all point to same address as predicted
print
tuple2 = (7, 7, 7)
for member in tuple2:
print member, id(member) # same as tuple1, 7 is still the same object
print
# look how Python stores tuple1 and tuple2, surprised?
print globals()
[/php]
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Thanks for clearing a few things up! We are all learning. I got to look in the Python source code what list really is patterned after. I am suspecting similarity to the C++ STL list rather than vector.
A cursory look at the source code shows the Python list to be much less advanced than the C++ STL list container. I am a little disappointed!
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Strings ar immutable like tuples, but have many functions to operate on them, while tuples do not. How is this?
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
I'm a Python newbie, but I'll take a stab:
The string methods like lower(), isdigit(), etc. all assume that the sequence consists of single characters. I think of a tuple as a string where each 'character' could be literally any object whatsoever. What would it mean to have a function like the following?
tup = (3,"hi", "4")
tup2 = tup.isalphanum()
Even reverse() could be ambiguous with tuples: Which is the correct output?
tup = ([1,2,3],[4,5,6])
tup.reverse()
([4,5,6],[1,2,3])
OR
tup = ([1,2,3],[4,5,6])
tup.reverse()
([6,5,4],[1,2,3])
so ... tuples have more flexible items, fewer dedicated methods than strings. But slices still work! :)
Regards,
Jeff Cagle
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156