Home » Swap Elements in list of Python

Swap Elements in list of Python

by Online Tutorials Library

Swap Elements in list of Python

So far, we have performed a variety of operations on lists in Python. In this article, we will learn how we can swap the elements of a list.

But first, let’s understand what swapping is all about?

Swapping is a process in which two variables exchange the values that they hold.

For example,

If a variable A holds an integer value of 10 and a variable B holds an integer value of 20, then on swapping B will hold 20 and A will hold 10.

We will use the following approaches to swap the elements in the list.

  1. The Basic Swap
  2. Using pop() function
  3. Using a tuple variable

So, let’s get started with the first one,

The Basic Swap

Here, we will the basic idea of swapping to swap the elements of list.

The following program illustrates the same,

Output:

The values inside the list are:  [20, 13, 41, 11, 10, 29]  The list with swapped elements is:  [20, 10, 41, 11, 13, 29]  

Explanation-

It’s time to understand what we have done in the above program,

  1. We have created a function that takes three parameters, list whose elements we would like to swap and a & b which are the indexes of elements.
  2. In the next step, we have simply used a comma and the assignment operator to show that the value inside ‘b’ goes in ‘a’ and vice versa.
  3. After this, we have initialized the list whose elements we would like to swap.
  4. We have then displayed the original list and specified the index values.
  5. Finally, we have passed these values inside our function and on execution the desired output is displayed.

In the second program, we shall see how pop() can be used here.

Using pop() function

Consider the program given below,

Output:

The values inside the list are:  [20, 13, 41, 11, 10, 29]  The list with swapped elements is:  [20, 10, 41, 11, 13, 29]  

Explanation-

It’s time to have a glance at the explanation of the above program,

  1. We have created a function that takes three parameters, list whose elements we would like to swap and a & b which are the indexes of elements.
  2. In the next step, we have removed the elements that we would like to swap and then inserted them back in the list at the exchanged positions.
  3. After this, we have initialized the list whose elements we would like to swap.
  4. We have then displayed the original list and specified the index values.
  5. Finally, we have passed these values inside our function and on execution the desired output is displayed.

Finally, we shall see how a tuple variable can be used to swap the elements of a list in Python.

Using a Tuple Variable

The program given below shows how it can be done,

Output:

The values inside the list are:  [20, 13, 41, 11, 10, 29]  The list with swapped elements is:  [20, 10, 41, 11, 13, 29]  

Explanation-

Let’s see what happened here,

  1. We have created a function that takes three parameters, list whose elements we would like to swap and a & b which are the indexes of elements.
  2. In the next step, we have used a tuple variable to store the elements present at positions a and b and then unpacked them as b and a in the list to obtain the resultant list with the swapped elements.
  3. After this, we have initialized the list whose elements we would like to swap.
  4. We have then displayed the original list and specified the index values.
  5. Finally, we have passed these values inside our function and on execution, the desired output is displayed.

Conclusion

In this tutorial, we learned what swapping is and how it can be implemented among the elements of a list present in Python.


Next TopicTitle in Python

You may also like