Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add a string to a deque without breaking the string up into characters?

Tags:

python

I am trying to create a deque of strings, but when I add a string to the deque it always breaks the string up into individual characters. Here is my code so far:

from collections import deque

my_string = "test"
my_queue = deque(my_string)

print my_queue

The output I get is:

deque(['t', 'e', 's', 't'])

I would like the output to be:

deque(['test'])

Any thoughts?

like image 936
Thomas Avatar asked May 04 '11 19:05

Thomas


People also ask

Is deque thread safe?

ConcurrentLinkedDeque is a lock-free implementation of Deque interface. This implementation is completely thread-safe as it uses an efficient lock-free algorithm.

Can you slice a deque?

Deques support indexing, but interestingly, they don't support slicing.

How do you separate the characters in a string?

First, define a string. Next, create a for-loop where the loop variable will start from index 0 and end at the length of the given string. Print the character present at every index in order to separate each individual character. For better visualization, separate each individual character by space.


1 Answers

The deque constructor takes an iterable as a parameter, if you just give the string to it, it will interpret it as a sequence of characters.

In order to do what you want you should wrap your string into a list:

your_string = 'string'
wrap_list = [your_string]
#Now create the deque
d = deque(wrap_list)

Of course you can do everything in one step:

your_string = 'string'
d = deque([your_string])
like image 146
Santiago Alessandri Avatar answered Sep 28 '22 18:09

Santiago Alessandri