By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,010 Members | 2,774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,010 IT Pros & Developers. It's quick & easy.

TUPLE & LIST

P: n/a
HI,

1) what are the differences between list and tuple?
2) how to concatenate tuple and list? no method, no opérator?
3) im looking the fucking manual, and cant add value in my tuple, when it
already created :/
how to do it?

thx.


Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Thu, 15 Jan 2004 15:08:42 +0100, in comp.lang.python you wrote:
HI,

1) what are the differences between list and tuple?
A list is a mutable (ie updatable) container for objects; you can add or
remove objects as much as you like. A tuple is an immutable (ie not
updatable) container, which, once created, cannot change.

The general idea is that a tuple is something like a record in Pascal or
a struct in C (without any named members), so the order of its contents
is important. For example, the function localtime of the time module
returns a tuple, whose first member is the year, second member is the
month etc.
OTOH, the order of some list's contents should bear no special meaning;
therefore a list has methods as .sort(), .reverse() etc.

Examples:
Use a list for a list of names.
Use a tuple for (x,y) coordinates in some area.
Use a list of (name,phone,address) tuples for your poor man's address
book which you will implement in python.
2) how to concatenate tuple and list? no method, no op?tor?
Convert either one to the type of the other.

If the final result should be a tuple, do something like:

final_tuple= your_tuple + tuple(your_list)

But you probably want a list, so do something like:

final_list= list(your_tuple) + your_list
3) im looking the fucking manual, and cant add value in my tuple, when it
already created :/
That's the whole idea, like the FM says in
http://www.python.org/doc/current/ref/types.html . A tuple cannot be
changed, therefore it consumes less space than a list and can be used as
a key, say, in a dictionary (unlike a list).
how to do it?


# You can't, but tuples can be concatenated:
a = 1,2,3
b = 4,5,6
print a (1, 2, 3) print b (4, 5, 6) print a+b (1, 2, 3, 4, 5, 6)

# or sliced:
print a[:2]+b[2:] (1, 2, 6)

# or indexed
print "the first member of %s is %s" % (a, a[0])


HTH
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix
Jul 18 '05 #2

P: n/a

"Yomanium Yoth Taripoät II" <Yo******@Yomanium.com> wrote in message
news:%7xNb.55
1) what are the differences between list and tuple? http://www.python.org/doc/faq/genera...ate-tuple-and-
list-data-types
2) how to concatenate tuple and list? no method, no opérator? listvar + list (tuplevar)
tuple (listvar) + tuplevar
3) im looking the fucking manual, and cant add value in my tuple, when it
already created :/
how to do it?

Try the Python Manual instead:
http://www.python.org/doc/current/li...q-mutable.html
I know it's not always easy to locate the documentation for the builtin
types.

Daniel

Jul 18 '05 #3

P: n/a
On Thursday 15 January 2004 2:08 pm, Yomanium Yoth Taripo� II wrote:
HI,

1) what are the differences between list and tuple?
2) how to concatenate tuple and list? no method, no op�ator?
3) im looking the fucking manual, and cant add value in my tuple, when it
already created :/
how to do it?

thx.


3) is your answer to 1) !
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
Jul 18 '05 #4

P: n/a
Hi Yomanium Yoth Taripoät II (cool name!),

Yomanium Yoth Taripoät II wrote:
1) what are the differences between list and tuple?
2) how to concatenate tuple and list? no method, no opérator?
3) im looking the fucking manual, and cant add value in my tuple, when it
already created :/
how to do it?


This question is answered in the FAQ:
http://www.python.org/doc/faq/genera...ist-data-types

Lists and tuples, while similar in many respects, are generally used in
fundamentally different ways. Tuples can be thought of as being similar
to Pascal records or C structs; they're small collections of related
data which may be of different types which are operated on as a group.
For example, a Cartesian coordinate is appropriately represented as a
tuple of two or three numbers.

Lists, on the other hand, are more like arrays in other languages. They
tend to hold a varying number of objects all of which have the same type
and which are operated on one-by-one. For example, os.listdir('.')
returns a list of strings representing the files in the current
directory. Functions which operate on this output would generally not
break if you added another file or two to the directory.

Tuples are immutable, meaning that once a tuple has been created, you
can't replace any of its elements with a new value. Lists are mutable,
meaning that you can always change a list's elements. Only immutable
elements can be used as dictionary keys, and hence only tuples and not
lists can be used as keys.

yours,
Gerrit.

--
198. If he put out the eye of a freed man, or break the bone of a freed
man, he shall pay one gold mina.
-- 1780 BC, Hammurabi, Code of Law
--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #5

P: n/a

"Yomanium Yoth Taripoät II" <Yo******@Yomanium.com> wrote in message
news:%7*************@newsr2.u-net.net...
HI,

1) what are the differences between list and tuple?
2) how to concatenate tuple and list? no method, no opérator?
3) im looking the manual, and can't add value in my tuple, when it
already created :/
how to do it?
Tuples are intended to be a quick way of passing
around several heterogenous values. If you find
yourself wanting to do anything to a tuple other than
creating it and accessing members, then you're
trying to do too much. Use a list if the values
are of the same type, or create an object to
hold whatever it is.

John Roth
thx.

Jul 18 '05 #6

P: n/a
Christos TZOTZIOY Georgiou wrote:
Examples:
Use a list for a list of names.
Use a tuple for (x,y) coordinates in some area.
Use a list of (name,phone,address) tuples for your poor man's address
book which you will implement in python.


What if someone moves away?
That is, I think one could better use a mutable type for the address book.

yours,
Gerrit.

--
46. If he do not receive a fixed rental for his field, but lets it on
half or third shares of the harvest, the grain on the field shall be
divided proportionately between the tiller and the owner.
-- 1780 BC, Hammurabi, Code of Law
--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #7

P: n/a

Gerrit Holl wrote in message ...
Christos TZOTZIOY Georgiou wrote:
Examples:
Use a list for a list of names.
Use a tuple for (x,y) coordinates in some area.
Use a list of (name,phone,address) tuples for your poor man's address
book which you will implement in python.


What if someone moves away?
That is, I think one could better use a mutable type for the address book.


That's why it's a "poor man's" address book. :)
--
Francis Avila
Jul 18 '05 #8

P: n/a
Yomanium Yoth Taripoät II wrote:
3) im looking the fucking manual, and cant add value in my tuple, when it already created :/

there's a manual for fucking that talks about tuples? sounds fun. do
share.

cheers,

// m

Jul 18 '05 #9

P: n/a
On Thu, 15 Jan 2004 14:18:36 -0500, rumours say that "Francis Avila"
<fr***********@yahoo.com> might have written:

Gerrit Holl wrote in message ...
Christos TZOTZIOY Georgiou wrote:
Examples:
Use a list for a list of names.
Use a tuple for (x,y) coordinates in some area.
Use a list of (name,phone,address) tuples for your poor man's address
book which you will implement in python.


What if someone moves away?
That is, I think one could better use a mutable type for the address book.


That's why it's a "poor man's" address book. :)


Thanks, Francis :)

To Gerrit: if the context was "help me make an address book", I believe
it is obvious I wouldn't suggest what I suggested. It was just an
example of concurrent tuple and list usage, where list stands for
database table and tuple for database row...
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.