[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

array and list

J. Peng

1/18/2008 3:23:00 AM

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

Ben Finney

1/18/2008 4:05:00 AM

0

"J. Peng" <peng.kyo@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

1/18/2008 4:37:00 AM

0

On Fri, 18 Jan 2008 15:05:23 +1100, Ben Finney wrote:

> "J. Peng" <peng.kyo@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

1/18/2008 8:07:00 AM

0

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.

P.S. if you know Perl then try: http://wiki.python.org/moin/PerlPhrasebook?highlight=...


- Paddy.

Ben Finney

1/18/2008 8:08:00 AM

0

Steven D'Aprano <steve@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

1/18/2008 8:19:00 AM

0


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

1/18/2008 9:22:00 AM

0

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

Bearophile

1/18/2008 9:48:00 AM

0

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

1/18/2008 2:42:00 PM

0


> 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

1/18/2008 6:53:00 PM

0



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

http://softwaremaven.inner...

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

Paul Rubin

1/18/2008 7:04:00 PM

0

Travis Jensen <travis.jensen@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.