简体   繁体   中英

Converting a deque object into list

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.

>>> list(collections.deque((1, 2, 3)))
[1, 2, 3]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM