Max size of list python

Recommended Answers

Hi bumsfeld,

I believe that the array data structure in the Numeric/NumPy package is both typed and non-resizable, just like C arrays [actually, I'm pretty sure that these data structures are C arrays wearing a Python hat]. Check out:

//numeric.scipy.org

Jump to Post

Thanks G-Do for your information, so NumPy eventually replaces Numeric and Numarray. Interesting!

When I was coding in C, I used to sample lab data and put it into a circular or ring buffer to keep the thing from overflowing. This was an easy way to calculate a moving …

Jump to Post

All 5 Replies

G-Do 19 Junior Poster

16 Years Ago

Hi bumsfeld,

I believe that the array data structure in the Numeric/NumPy package is both typed and non-resizable, just like C arrays [actually, I'm pretty sure that these data structures are C arrays wearing a Python hat]. Check out:

//numeric.scipy.org

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

16 Years Ago

Thanks G-Do for your information, so NumPy eventually replaces Numeric and Numarray. Interesting!

When I was coding in C, I used to sample lab data and put it into a circular or ring buffer to keep the thing from overflowing. This was an easy way to calculate a moving average.

You can do a similar thing in Python using a list and keeping track of the index. When the index reaches the limit you have set, new data starts at index zero again.

A queue makes this simpler, particularly the deque container, because you can add to the end and pop the front. Here is an example, I hope you can see what is happening:

# the equivalent of a circular size limited list
# also known as ring buffer, pops the oldest data item
# to make room for newest data item when max size is reached
# uses the double ended queue available in Python24
 
from collections import deque
 
class RingBuffer[deque]:
    """
    inherits deque, pops the oldest data to make room
    for the newest data when size is reached
    """
    def __init__[self, size]:
        deque.__init__[self]
        self.size = size
        
    def full_append[self, item]:
        deque.append[self, item]
        # full, pop the oldest item, left most item
        self.popleft[]
        
    def append[self, item]:
        deque.append[self, item]
        # max size reached, append becomes full_append
        if len[self] == self.size:
            self.append = self.full_append
    
    def get[self]:
        """returns a list of size items [newest items]"""
        return list[self]

    
# testing
if __name__ == '__main__':
    size = 5
    ring = RingBuffer[size]
    for x in range[9]:
        ring.append[x]
        print ring.get[]  # test
 
"""
notice that the left most item is popped to make room
result =
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
[3, 4, 5, 6, 7]
[4, 5, 6, 7, 8]
"""

bumsfeld 413 Nearly a Posting Virtuoso

16 Years Ago

So the trick is to make size of ring buffer large enough to retain all pertinent data long enough?
I guess the answer is yes.

14 Years Ago

Why not something like:

if len[mylist] > 5:
...del mylist[0]

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

14 Years Ago

Why not something like:

if len[mylist] > 5:
...del mylist[0]

Good idea, but if mylist is appended to in several different places in your code than it would not be very practical.

Reply to this topic

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.

What is the size of list in Python?

A list is identifiable by the square brackets that surround it, and individual values are separated by a comma. To get the length of a list in Python, you can use the built-in len[] function. Apart from the len[] function, you can also use a for loop and the length_hint[] function to get the length of a list.

How do I set the maximum length of a list?

How to set a maximum length for a python list/set? One approach is to create custom class of list and inherit functionality from python list . Then in add [and maybe others] methods add a check for max length. @stalk you should post that as an answer.

What is the maximum size of an array in Python?

32-bit: the value will be 2^31 – 1, i.e. 2147483647. 64-bit: the value will be 2^63 – 1, i.e. 9223372036854775807.

Can we increase the size of list in Python?

The . extend[] method increases the length of the list by the number of elements that are provided to the method, so if you want to add multiple elements to the list, you can use this method.

Chủ Đề