Search code examples
python-3.xstringdeque

Deque to string in Python3


In Python2 I was able to do something similar to:

frames = deque(maxlen=xyz)
framesString = ''.join(frames)

In Python3 I get an error.

How should I change it in order to get a string representing the deque object?

Thanks in advance, G.


Solution

  • From Python official documentation, you can use deque like this:

    from collections import deque
    
    # With an infinite length
    frames = deque('ytreza')
    ''.join(frames)
    # It will display 'azerty'
    
    # With a maximum length
    frames = deque('ytreza', maxlen=3)
    ''.join(frames)
    # It will display 'aze'
    
    # With no input
    assert len(deque(maxlen=3)) == 0
    ''.join(deque(maxlen(3)))
    # It will display an empty string    
    

    If content of the deque is bytes, a str conversion should be performed, with an expression generator for example.

    ''.join(str(element) for element in my_deque)