Limiting the size of List and making it Homogeneous

A

ankit

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
 
E

Erik Max Francis

ankit said:
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.
 
G

gene tani

ankit said:
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
 
G

gene tani

gene said:
ankit said:
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.
 
B

bearophileHUGS

The array module allows you to specify a single type of elements.

Bye,
bearophile
 
M

Mike C. Fletcher

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/basictypes.rlist.html
http://cvs.sourceforge.net/viewcvs.py/basicproperty/properties/basictypes/rlist.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
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top