Search code examples
pythondeque

How can we extend negative numbers to left side in a deque in python?


If I type the following code in python 3 to extend the negative numbers to the left of the deque

de=collections.deque([])
de.extendleft('-1-2-3-4-5-6')

I'm getting an output like this

deque(['6', '-', '5', '-', '4', '-', '3', '-', '2', '-', '1', '-'])

instead I want a output like:

deque(['-6','-5''-4','-3','-2','-1'])

Solution

  • You are giving string as input. Because of that it's considering - this also a element.

    from collections import deque
    
    x = '-1-2-3-4-5-6'.replace('-', ' -').strip()
    de = deque([])
    de.extendleft(x.split())