7

I am currently creating my deque object using the following,

self.CommandList = deque((['S', False, 60],['c'],['g16'],['i50'],['r30', True],['u320'],['o5000'],['b1'],['B4500'],['W1'],['l5154'],['!10'],['p2', True, 10],['e1'],['K20'],['U0'],['Y0']))

But I wish to add a similar list to the queue later but using appendleft, so it can jump ahead of the list. I assumed the following, but had no luck.

NewList = (['S'],['c'],['!10'],['p4'],['o1200'],['i50'],['r30'],['b10'],['d1'],['A', True, 163])
self.CommandList.appendleft(NewList)

Is this even possible with appendleft?

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Schodemeiss
  • 1,144
  • 3
  • 16
  • 37

1 Answers1

20

I think you want .extendleft here. This will "extend the list" instead of just appending the list as one element.

z = collections.deque([1,2,3,4])   # [1, 2, 3, 4]

z.appendleft(['bad', 'news'])   # [ ['bad', 'news'], 1, 2, 3, 4 ]
z.extendleft(['good', 'news'])  # [ 'good', 'news', ['bad', 'news'], 1, 2, 3, 4 ]

If they are getting inserted in reverse, the quick fix is to just reverse the list:

z.extendleft(reversed(['good', 'news']))
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Donald Miner
  • 38,889
  • 8
  • 95
  • 118
  • Hi. Because I am using multiple lists they actually get added in reverse! For example: self.CommandList.extendleft(['S'],['c'],['!10'],['p4']) ends up with p4 being first! How do I get around this? – Schodemeiss Jul 15 '10 at 14:23