Swapping of subsets in the same list using Python

Viewed 67

What's wrong with the code below? I am trying to swap two subsets of list of strings.

>>> a = ['b', 'b', 'b', 'a', 'x', 'x', 'x', 'x', 'x', 'y']  
['b', 'b', 'b', 'a', 'x', 'x', 'x', 'x', 'x', 'y']
>>> a[4:9]
['x', 'x', 'x', 'x', 'x']
>>> a[9:10]  
['y']
>>> a[4:9], a[9:10] = a[9:10], a[4:9]
>>> a  
['b', 'b', 'b', 'a', 'y', 'y', 'x', 'x', 'x', 'x', 'x']
2 Answers
Related