This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Deque with iterable object as one object
Type: performance Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: jonathandaniel, rhettinger
Priority: normal Keywords:

Created on 2018-01-18 19:53 by jonathandaniel, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg310256 - (view) Author: Jonathan (jonathandaniel) Date: 2018-01-18 19:53
Creating a deque with an iterable object creates extra overhead if you want to insert it as one element.

e.g:
import timeit

test1 = '''
str = "x" * 100000
lst = [str]
'''

test2 = '''
str = "x" * 100000
'''

print(timeit.timeit(test1, number=100000))
print(timeit.timeit(test2, number=100000))

If I want to add a string to a deque i will have to put it in a list first, this causes 23% slower performance in my case.

There should be a deque where this overhead is not needed because in most cases this will be used in performance demanding cases.
msg310257 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-01-18 20:27
Sorry Jonathan, this is the way the python containers work if they take an iterable input.  In the case of a str, it is not possible for us to know whether you mean for deque('abc') to go it as three arguments or as one.

FWIW, if you don't what to put the single element in a list, the API provides the append() method for adding scalars and extend() method for adding iterables:

   d = deque()
   d.append('abc')
   d.extend('abc')

Note that lists behave the same way.
msg310269 - (view) Author: Jonathan (jonathandaniel) Date: 2018-01-19 09:08
Hello,

I dont know why but yesterday when i appended it behaved like extend.
I should sleep more.

2018-01-18 20:27 GMT+00:00 Raymond Hettinger <report@bugs.python.org>:

>
> Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:
>
> Sorry Jonathan, this is the way the python containers work if they take an
> iterable input.  In the case of a str, it is not possible for us to know
> whether you mean for deque('abc') to go it as three arguments or as one.
>
> FWIW, if you don't what to put the single element in a list, the API
> provides the append() method for adding scalars and extend() method for
> adding iterables:
>
>    d = deque()
>    d.append('abc')
>    d.extend('abc')
>
> Note that lists behave the same way.
>
> ----------
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue32595>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76776
2018-01-19 09:08:45jonathandanielsetmessages: + msg310269
2018-01-18 20:27:49rhettingersetstatus: open -> closed
resolution: not a bug
messages: + msg310257

stage: resolved
2018-01-18 19:56:45rhettingersetassignee: rhettinger

nosy: + rhettinger
2018-01-18 19:53:57jonathandanielcreate