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

array and list

P: n/a
what's the difference between an array and a list in python?
I see list has all features of array in C or perl.
so please tell me.thanks.
Jan 18 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"J. Peng" <pe******@gmail.comwrites:
what's the difference between an array and a list in python?
In Python, 'list' is a basic built-in type. Python has no 'array'
type, though that term is often used to refer to the 'array' type
defined in Numeric Python (which is not part of the standard library,
so not really part of Python).
I see list has all features of array in C or perl.
You may also want to compare and constrast Python 'list' and 'dict'.

--
\ "When cryptography is outlawed, bayl bhgynjf jvyy unir |
`\ cevinpl." -- Anonymous |
_o__) |
Ben Finney
Jan 18 '08 #2

P: n/a
On Fri, 18 Jan 2008 15:05:23 +1100, Ben Finney wrote:
"J. Peng" <pe******@gmail.comwrites:
>what's the difference between an array and a list in python?

In Python, 'list' is a basic built-in type. Python has no 'array' type,
though that term is often used to refer to the 'array' type defined in
Numeric Python (which is not part of the standard library, so not really
part of Python).

Did you forget the array module?

>>import array
array
<module 'array' from '/usr/lib/python2.5/lib-dynload/arraymodule.so'>

>I see list has all features of array in C or perl.

You may also want to compare and constrast Python 'list' and 'dict'.
The Original Poster might also like to check out the help() and dir()
functions in the interactive interpreter:

help(list)
dir(list)
help(array)
help(array.array)

etc.


--
Steven
Jan 18 '08 #3

P: n/a
On Jan 18, 3:23 am, "J. Peng" <peng....@gmail.comwrote:
what's the difference between an array and a list in python?
I see list has all features of array in C or perl.
so please tell me.thanks.
If you are new to Python, then where other languages may reach for an
'array', Python programs might organise data as lists. Lists are used
much more than arrays in Python. you should find that is the case in
tutorials/books too.

P.S. if you know Perl then try: http://wiki.python.org/moin/PerlPhra...ght=%28perl%29
- Paddy.
Jan 18 '08 #4

P: n/a
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com.auwrites:
On Fri, 18 Jan 2008 15:05:23 +1100, Ben Finney wrote:
In Python, 'list' is a basic built-in type. Python has no 'array'
type [...]
Did you forget the array module?
Yes.

--
\ "Always code as if the guy who ends up maintaining your code |
`\ will be a violent psychopath who knows where you live." —John |
_o__) F. Woods |
Ben Finney
Jan 18 '08 #5

P: n/a
On Jan 18, 3:23 am, "J. Peng" <peng....@gmail.comwrote:
>what's the difference between an array and a list in python?
I see list has all features of array in C or perl.
so please tell me.thanks.

If you are new to Python, then where other languages may reach for an
'array', Python programs might organise data as lists. Lists are used
much more than arrays in Python. you should find that is the case in
tutorials/books too.

http://wiki.python.org/moin/PerlPhra...ght=%28perl%29
- Paddy.
Hi,

From Core Python Programming book (btw I like this book) I know the
difference is that array can hold only one type (the C standard?), but
list can hold every type of object. But hmm, perl's array also can hold
every type of variables, why perl call it array and python call it list?
Also, if I understand it correctly, python's tuple is called as 'list'
in perl, so I'm somewhat confused about them.
P.S. if you know Perl then try:
Yes I know some Perl. I have registered module on CPAN.

thanks!

Jan 18 '08 #6

P: n/a
On Jan 18, 8:18 am, "J. Peng" <jp...@block.duxieweb.comwrote:
On Jan 18, 3:23 am, "J. Peng" <peng....@gmail.comwrote:
what's the difference between an array and a list in python?
I see list has all features of array in C or perl.
so please tell me.thanks.
If you are new to Python, then where other languages may reach for an
'array', Python programs might organise data as lists. Lists are used
much more than arrays in Python. you should find that is the case in
tutorials/books too.
http://wiki.python.org/moin/PerlPhra...ght=%28perl%29
- Paddy.

Hi,

From Core Python Programming book (btw I like this book) I know the
difference is that array can hold only one type (the C standard?), but
list can hold every type of object. But hmm, perl's array also can hold
every type of variables, why perl call it array and python call it list?
Similar ideas, different names. It happens. I guess 'under the hood'
Python
(& Perl?), arrays might be more like an implementation of what the C
programmer might call a linked list, but even then there are
differences as
most linked list examples given in C tutorials are lists of the same
type of
object,....

- Paddy.
Also, if I understand it correctly, python's tuple is called as 'list'
in perl, so I'm somewhat confused about them.
P.S. if you know Perl then try:

Yes I know some Perl. I have registered module on CPAN.

thanks!
Jan 18 '08 #7

P: n/a
J. Peng>why perl call it array and python call it list?<

Unfortunate naming, I'd say. Calling list a dynamic array is silly,
despite all the things you may say about abstract data types having
the correct methods, etc.
Paddy:
I guess 'under the hood' Python (& Perl?), arrays might be more like
an implementation of what the C programmer might call a linked list,
but even then there are differences as most linked list examples
given in C tutorials are lists of the same type of object,....
Both Python "array.array" and "list" are implemented as dyn arrays,
not as lists. Only the good "deque" by Hettinger is something similar
to a list (of small arrays).

Bye,
bearophile
Jan 18 '08 #8

P: n/a
Paddy:
I guess 'under the hood' Python (& Perl?), arrays might be more like
an implementation of what the C programmer might call a linked list,
but even then there are differences as most linked list examples
given in C tutorials are lists of the same type of object,....

Both Python "array.array" and "list" are implemented as dyn arrays,
not as lists. Only the good "deque" by Hettinger is something similar
to a list (of small arrays).

Bye,
bearophile
Ta!
Jan 18 '08 #9

P: n/a


On Jan 18, 2008, at 2:48 AM, be************@lycos.com wrote:
J. Peng>why perl call it array and python call it list?<

Unfortunate naming, I'd say. Calling list a dynamic array is silly,
despite all the things you may say about abstract data types having
the correct methods, etc.

I wouldn't call it unfortunate. The list semantics follow LISP's
semantics far more closely than C's array semantics. I would hazard
to guess that they are called lists because that is what every
functional language calls them and this aspect of python was modeled
after functional languages.

tj

Travis Jensen
tr***********@gmail.com

http://softwaremaven.innerbrane.com/

You should read my blog; it is more interesting than my signiature.

Jan 18 '08 #10

P: n/a
Travis Jensen <tr***********@gmail.comwrites:
I wouldn't call it unfortunate. The list semantics follow LISP's
semantics far more closely than C's array semantics. I would hazard
to guess that they are called lists because that is what every
functional language calls them and this aspect of python was modeled
after functional languages.
1. I don't think python was modelled after functional languages--it has
always been imperative, while slowly gaining functional features,
sometimes against opposition.

2. In functional langagues, "list" traditionally means a linked
structure while "array" is a linear structure, so python's use of
the term "list" is actually a little bit confusing.
Jan 18 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.