57

Currently, I fetch "list" data from my storage, "deque" it to work with that data.

After processing the fetched data, I have to put them back into the storage. This won't be a problem as long as I am not forced to use Python's standard "list" object to save this data.

Storage Service: Google Appengine.

My work-around would be:

dequeObj = deque(myData)
my_list = list()
for obj in dequeObj:
    my_list.append(obj)

but this seems not very optimal.

cottontail
  • 10,268
  • 18
  • 50
  • 51
Julius F
  • 3,434
  • 4
  • 29
  • 44

2 Answers2

114
>>> list(collections.deque((1, 2, 3)))
[1, 2, 3]
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    nb. This works because a deque is iterable, and `list()` will make list from an iterable. (eg. `list(range(5))`) – pjz Mar 15 '21 at 21:16
0

Since deques are iterables, you can also unpack it inside a list.

dq = collections.deque([1, 2, 3])
lst = [*dq]
lst             # [1, 2, 3]

To create a new list object, you can even pack the deque into a variable as well.

*lst, = dq
lst             # [1, 2, 3]
cottontail
  • 10,268
  • 18
  • 50
  • 51