Assume you have a list A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. In a single line of code, cut A in half and assign B to be a copy of A’s last half followed by the first half (i.e. B would be equal to [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]).
- 
                    5Start here: http://stackoverflow.com/help/how-to-ask – ppovoski Jan 13 '17 at 23:31
3 Answers
You can slice the list based on the length as:
>>> A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> B = A[-len(A)//2:] + A[:len(A)//2]
>>> B
[6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
Refer linked post to know more about Python's slice notation
 
    
    - 1
- 1
 
    
    - 46,825
- 13
- 96
- 126
- 
                    1Just a quick note: `/2` returns a float on python3 and thus throws an error when you try to slice with it. You could use `//` to make it work on both py2 and py3 (see also my answer). – MSeifert Jan 14 '17 at 18:28
The three steps to solve this problems:
- Calculating the mid. I assume that for unqueal sized lists the second half should be longer. Then you can simply use floor division: - len(A) // 2.
- You can slice from a specific index ( - [idx:]) and you can slice up to an index (- [:idx]). These are just two applications of slicing, but the ones you need here.
- You can concatenate lists by adding ( - +) them.
Putting all this together:
B = A[len(A)//2:] + A[:len(A)//2]   # second half + first half
 
    
    - 145,886
- 38
- 333
- 352
The answer to your exact question with the list [1,2,3,4,5,6,7,8,9,10] is: split the list a from index 5 till the last and add the elements with index with 0 till 4. You can do this with the following code:
a=[1,2,3,4,5,6,7,8,9,10]
b=a[5:]+a[0:5]
In general, in the sense, with n number of elements, the program can be written as:
a=[1,2,3,4,5,6,7,8,9,10]
l=len(a)
b=a[l/2:]+[0:l/2]
print b
 
    
    - 1,148
- 11
- 20
- 
                    If you use python3 your second example fails because `/2` returns a float. You can use `//` to force it to be an integer on py2 and py3 (see also my answer). – MSeifert Jan 14 '17 at 18:27
