Q:

Python: Deque[-1] = Deque.pop() giving interesting result

I was working with a deque and was wondering why the following code works the way it does
Code:
import collections

d = collections.deque()
d.append("a")
d.append("b")
d[-1] += d.pop()
print(d)

Output:
deque(['bb'])
This surprised me, as I would have expected the code to result in deque(['ab']) instead of deque(['bb']).  Why did this occur? 

A:

this line
d[-1] += d.pop()

is equal by this line
d[-1] = d[-1] + d.pop()

python first get d[-1] and that is 'b'
after that run pop method for d and this method return 'b'
at the end python set 'bb' for d[-1]
if you want pop first item use popleft() function. 
edited code:
from collections import deque

d = deque()

d.append("a")
d.append("b")

d[-1] = d.popleft() + d[-1]

print(d)

output:
deque(['ab'])

