Why does deque.extendleft () reverse the order of the elements?

From the official documentation:

extendleft (iteration)

   Extend the left side of the deque by appending elements from iterable. Note, the 
   series of left appends results in reversing the order of elements in the iterable argument.

      

What is the reason / concept for the behavior of this method? I know I can easily change it using reversed

, but I'm just curious.

+3


source to share


1 answer


Let's say you expand with [1, 2, 3, 4]

. The iterable will be iterated over and each element will be added to the left. So the process looks like this

Read the first element and put it to the left

1

      

Read the second item and put it to the left

2 -> 1

      

Read the third item and put it to the left



3 -> 2 -> 1

      

Read the fourth item and place it to the left

4 -> 3 -> 2 -> 1

      

See, item order reversed. In fact, it seems logical to me. When we do list.extend

like this

[].extend([1, 2, 3, 4])

      

The iterable is repeated and added to the right end of the list. Thus, the pattern follows extendleft

.

+3


source







All Articles