## Saturday, October 17, 2020

Solution Exercise 2: python algorithm to swap a two elements in given list

#### Exercise 2

write python algorithm to swap the first element with the last element of given list. Example: if L =
``["Python" , "Java" , "C++" , "Javascript"]``
, the algorithm returns the list:
``["Javascript",  "Java" , "C++" , "Python"]``

#### Solution

``````def swapList(L):
# getting the last element of list
swap = L[-1]

# replace the last element by the first element of list
L[-1] = L[0]

# relace the first element by the last element
L[0] = swap
return L

# Example
L = L = ["Python" , "Java" , "C++" , "Javascript"]
print(swapList(L))
# The output is : ['Javascript', 'Java', 'C++', 'Python']``````

Younes Derfoufi
my-courses.net

## Python Exercises With Solutions

1. Python Arithmetic Exercises
2. Equations & System Of Equations
3. Polynomials and Functions
4. Python Arithmetic Exercises
5. Numpy, Matplolib, Sympy , Scipy - Exercises
6. Python Arithmetic Exercises

Younes Derfoufi