Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

array and list

5 views
Skip to first unread message

J. Peng

unread,
Jan 17, 2008, 10:23:18 PM1/17/08
to pytho...@python.org
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.

Ben Finney

unread,
Jan 17, 2008, 11:05:23 PM1/17/08
to
"J. Peng" <peng...@gmail.com> writes:

> 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

Steven D'Aprano

unread,
Jan 17, 2008, 11:36:35 PM1/17/08
to
On Fri, 18 Jan 2008 15:05:23 +1100, Ben Finney wrote:

> "J. Peng" <peng...@gmail.com> writes:
>
>> 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

Paddy

unread,
Jan 18, 2008, 3:06:41 AM1/18/08
to

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/PerlPhrasebook?highlight=%28perl%29


- Paddy.

Ben Finney

unread,
Jan 18, 2008, 3:07:51 AM1/18/08
to
Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au> writes:

> 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

J. Peng

unread,
Jan 18, 2008, 3:18:30 AM1/18/08
to pytho...@python.org

> On Jan 18, 3:23 am, "J. Peng" <peng....@gmail.com> wrote:
>
>> 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/PerlPhrasebook?highlight=%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!

Paddy

unread,
Jan 18, 2008, 4:21:57 AM1/18/08
to
On Jan 18, 8:18 am, "J. Peng" <jp...@block.duxieweb.com> wrote:
> > On Jan 18, 3:23 am, "J. Peng" <peng....@gmail.com> wrote:
>
> >> 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/PerlPhrasebook?highlight=%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.

bearoph...@lycos.com

unread,
Jan 18, 2008, 4:48:21 AM1/18/08
to
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

Paddy

unread,
Jan 18, 2008, 9:41:40 AM1/18/08
to

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

Travis Jensen

unread,
Jan 18, 2008, 1:53:23 PM1/18/08
to pytho...@python.org

On Jan 18, 2008, at 2:48 AM, bearoph...@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
travis...@gmail.com

http://softwaremaven.innerbrane.com/

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

Paul Rubin

unread,
Jan 18, 2008, 2:03:47 PM1/18/08
to
Travis Jensen <travis...@gmail.com> writes:
> 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.

0 new messages