TUPLE & LIST

  • Thread starter Yomanium Yoth Taripoät II
  • Start date
Y

Yomanium Yoth Taripoät II

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

James Henderson

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) !
 
G

Gerrit Holl

Hi Yomanium Yoth Taripoät II (cool name!),
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/general.html#why-are-there-separate-tuple-and-list-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.
 
D

Daniel Dittmar

Yomanium Yoth Taripoät II said:
1) what are the differences between list and tuple? http://www.python.org/doc/faq/general.html#why-are-there-separate-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/lib/typesseq-mutable.html
I know it's not always easy to locate the documentation for the builtin
types.

Daniel
 
C

Christos TZOTZIOY Georgiou

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:
(1, 2, 3, 4, 5, 6)

# or sliced:
(1, 2, 6)

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

HTH
 
J

John Roth

Yomanium Yoth Taripoät II said:
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
 
G

Gerrit Holl

Christos said:
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.
 
F

Francis Avila

Gerrit Holl wrote in message ...
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. :)
 
M

Mark McEahern

Yomanium said:
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
 
C

Christos TZOTZIOY Georgiou

Gerrit Holl wrote in message ...

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,945
Members
47,492
Latest member
gabbywilliam

Latest Threads

Top