Python-courses-python-exercises-python-exception-python-pip

Exercise 32

Write a Python program that move the first 3 elements of a given list and place them at the end of the list. Example if L = [25, 13, 11, 1, 4, 31, 18, 6, 12, 1, 7], the program returns: the list: [1, 4, 31, 18, 6, 12, 1, 7, 25, 13, 11]

Solution

def moveListElements (L):
# remove the three elements from the list
L1 = L [3 ::]
# extraction of the first three elements
L2 = L [0: 3]

return L1 + L2

L = [25, 13, 11, 1, 4, 31, 18, 6, 12, 1, 7]
print (moveListElements(L))
# The output is: [1, 4, 31, 18, 6, 12, 1, 7, 25, 13, 11]
 

Younes Derfoufi
my-courses.net

Leave a Reply