-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
deque object isn't iterable #4776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If it's written that it's list-like, that doesn't mean that it behaves all like list, and docs are explicit in what functionality it provides. If you need list, use list. The purpose of objects like ucollections.deque is to do what lists can't do, in the minimal code size. |
But wouldn't it be useful to access the contents of the deque? |
The collections.deque from CPython makes it very easy to implement a fast moving average for real-time data (for example, for averaging data from some sensor): deq = collections.deque(window_size)
while true:
sample = sensor.get_data()
deq.append(sample)
mov_aver = sum(deq) / len(deq) So it would be very useful to make the ucollections.deque iterable also. |
I just ran into the exact same issue.
|
This would make deque much more useful |
The version here in micropython-lib is iterable. The class has an >>> d = deque((5,1,9,2))
>>> sum(d)
17
>>> max(d)
9
>>> for n in d:
... print(n)
...
5
1
9
2
>>> |
thanks @peterhinch for linking to the deque.py in the micropython-lib. An hour back I imported collections.deque on my rpi pico and got a different version with no iter. ( v1.19.1 on 2022-06-18). to use it for the first time. I'd be quite lost without this info. (very helpful indeed) |
For some background: There is a built-in version of deque written in C that supports There's a more full-featured version of deque in micropython-lib that is written in Python and wraps up a list. micropython/micropython-lib#440 will add more features. This version is slower, takes more space but is becoming close to feature-complete. The C built-in is packaged with each release of MicroPython but the MicroPython-lib version needs to be manually installed. Moving forward, I'd like to see the MicroPython-lib version reach feature parity with CPython as well as having tests added. In parallel we should judiciously add more features to the built-in; taking care since there is a cost in terms of flash for every build. Another alternative that has been discussed is to add deque as a native module. No work has started on this effort yet, to my knowledge. |
Ran into this issue as well trying to implement a running average :( |
I would consider a circular buffer for this task: it avoids repeated allocation. |
Link to #10724. |
As of 7dff38f, deque is iterable. |
MicroPython v1.10 on 2019-01-25; ESP32 module with ESP32
fails with
TypeError: 'deque' object isn't iterable
prints
['__class__', 'append', 'popleft']
Documentation:
Deques (double-ended queues) are a list-like container, however lists behave different:
The text was updated successfully, but these errors were encountered: