469,328 Members | 1,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Limiting the size of List and making it Homogeneous


Is it possible to limit the size of list in python.
I want to make list of 5 elements. How can I achieve this thing in
python. And one more thing can we declare list to store elements of
same type as in c, C++ we can declare an
array which can have 5 elements of type int.
C, C++:
int intarr[5]
How can I achieve this kind of behavior ?

Thanks

Jan 13 '06 #1
5 2018
ankit wrote:
Is it possible to limit the size of list in python.
I want to make list of 5 elements. How can I achieve this thing in
python. And one more thing can we declare list to store elements of
same type as in c, C++ we can declare an
array which can have 5 elements of type int.
C, C++:
int intarr[5]
How can I achieve this kind of behavior ?


Use a list and keep it of length 5.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
There are countless planets, like many island Earths ...
-- Konstantin Tsiolkovsky
Jan 13 '06 #2

ankit wrote:
Is it possible to limit the size of list in python.
I want to make list of 5 elements. How can I achieve this thing in
python. And one more thing can we declare list to store elements of
same type as in c, C++ we can declare an
array which can have 5 elements of type int.
C, C++:
int intarr[5]
How can I achieve this kind of behavior ?

Thanks


- ring(circular) buffers overwrite oldest elements when they're flagged
as at their limit, is that what you want?:

http://www.onlamp.com/lpt/a/5828

you can write whatever type checks you want in the append() methods

Jan 13 '06 #3

gene tani wrote:
ankit wrote:
Is it possible to limit the size of list in python.
I want to make list of 5 elements. How can I achieve this thing in
python. And one more thing can we declare list to store elements of
same type as in c, C++ we can declare an
array which can have 5 elements of type int.
C, C++:
int intarr[5]
How can I achieve this kind of behavior ?

Thanks


- ring(circular) buffers overwrite oldest elements when they're flagged
as at their limit, is that what you want?:

http://www.onlamp.com/lpt/a/5828

you can write whatever type checks you want in the append() methods


the other possible interpret what you're asking is these lists are
read-only so you subclass list and your __init__() either truncates any
more than 5 list elements passed to it, or throws an exception when
this happens. At this point you've hit the limits of my ability to
read your mind.

Jan 13 '06 #4
The array module allows you to specify a single type of elements.

Bye,
bearophile

Jan 13 '06 #5
Little class that does restrictions/checks on adding to a list (simply
subclass and put your restrictions in the beforeAdd or beforeMultipleAdd
methods).

http://pytable.sourceforge.net/pydoc...pes.rlist.html
http://cvs.sourceforge.net/viewcvs.p...py?view=markup

Note, however, that if you goal is to create a simple C-level pointer of
machine ints you'll need to use Numpy or a similar system that
implements such a type.

Have fun,
Mike

ankit wrote:
Is it possible to limit the size of list in python.
I want to make list of 5 elements. How can I achieve this thing in
python. And one more thing can we declare list to store elements of
same type as in c, C++ we can declare an
array which can have 5 elements of type int.
C, C++:
int intarr[5]
How can I achieve this kind of behavior ?

Thanks

--
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Jan 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Mike | last post: by
12 posts views Thread by Brett L. Moore | last post: by
25 posts views Thread by Matthias | last post: by
5 posts views Thread by Campano, Troy | last post: by
26 posts views Thread by Lionel B | last post: by
4 posts views Thread by Dimitrios Apostolou | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.