944,038 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 10544
  • Python RSS
Oct 14th, 2005
0

Tuple List

Expand Post »
Why is there a tuple and list in Python. They seemed to perform similar things.
Similar Threads
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 14th, 2005
0

Re: Tuple List

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 16th, 2005
0

Re: Tuple List

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?
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Oct 17th, 2005
0

Re: Tuple List

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]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 29th, 2006
0

Re: Tuple List

I think you're confusing a number of Python's concepts. The difference between lists and tuples is that lists are mutable (modifyable) and tuples are immutable (never-changing). For example:
Python Syntax (Toggle Plain Text)
  1. >>> myList = [1,2,3]
  2. >>> myList[1] = 3
  3. >>> myTuple = (1,2,3)
  4. >>> myTuple[1] = 3
  5. Traceback (most recent call last):
  6. File "<stdin>", line 1, in ?
  7. TypeError: object doesn't support item assignment
  8.  
That's it. That's the big difference. Use tuples for when things shouldn't change, and lists for when they should. Tuples are thus faster (they don't have to worry about growing, shrinking, etc) while lists are more flexible.

Secondly, a list is NOT implemented as a linked list. A list is a growable array, like an C++'s std::vector. They're called lists because they are lists of items, in the non-computer-science way. (Not to mention list doesn't imply linked list, it's just more common to see linked list than any other useage of list)

Thirdly, *everything* in Python is stored as a reference (or pointer, if that's the term you prefer). Everything is also an object. Integers and Integers, like tuples, are immutable. The function id returns "the identity of an object. This is guaranteed to be unique among simultaneously existing objects." It is true that under the normal python interpreter this is a pointer to the object. So each instance of an object would have its own unique id, then why did all of the 7s have the same id? This is because python stores the first 100 integers internally, and anytime they are created it will just reference the existing immortal version. This is one of a few optimizations Python makes that will make the id function return less than helpful results. The other is that most constants (strings or integers) will end up being the same instance (since you can't *ever* change them, they can be shared as much as possible with no harmful side effects)

So to make a long story short, keep lists around when you want to add or remove items, or modify item order. Keep tuples around when they're not going to change (or at least not often).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TimothyFitz is offline Offline
2 posts
since Jun 2006
Jun 30th, 2006
0

Re: Tuple List

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!
Last edited by vegaseat; Jul 1st, 2006 at 12:24 am.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 2nd, 2006
0

Re: Tuple List

Strings ar immutable like tuples, but have many functions to operate on them, while tuples do not. How is this?
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jul 14th, 2006
0

Re: Tuple List

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

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: biopython
Next Thread in Python Forum Timeline: Tkinter Help





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


Follow us on Twitter


© 2011 DaniWeb® LLC