• We just launched and are currently in beta. Join us as we build and grow the community.

How to Swap Element in List using Python

oliverioRS

Shell Master
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
121
Likes
17
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, we will program 'How to Swap Elements in a List using Python.' We will learn how to swap elements within a list. The objective is to quickly swap two elements in the list. I will provide a sample program to demonstrate the actual coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you will be able to do it yourself with ease. The program I will show you covers the basics of programming for swapping elements in a list. I will do my best to provide you with a simple method for changing the index positions of elements in a list. So, let's start coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. def

    swapElement(

    list

    ,

    pos1,

    pos2)

    :

  2. list

    [

    pos1]

    ,

    list

    [

    pos2]

    =

    list

    [

    pos2]

    ,

    list

    [

    pos1]
  3. return

    list


  4. ret =

    False


  5. while

    True

    :
  6. print

    (

    "\n

    ================= Swap Element in List =================\n

    \n

    "

    )


  7. List =

    [

    53

    ,

    21

    ,

    54

    ,

    34

    ,

    34

    ]
  8. pos1,

    pos2 =

    2

    ,

    5

  9. print

    (

    "Original List: "

    ,

    List)
  10. print

    (

    "\n

    "

    )
  11. print

    (

    "Swap List: "

    ,

    swapElement(

    List,

    pos1-1

    ,

    pos2-1

    )

    )

  12. opt =

    input

    (

    "\n

    Do you want to try again?(yes/no): "

    )

  13. if

    opt.lower

    (

    )

    ==

    'yes'

    :
  14. ret=

    False
  15. elif

    opt.lower

    (

    )

    ==

    'no'

    :
  16. ret=

    True
  17. print

    (

    "Exiting program...."

    )

  18. else

    :
  19. print

    (

    "Please enter yes/no:"

    )
  20. break

  21. if

    ret ==

    False

    :
  22. continue

This Python script swaps two elements in a list. The swapElement function takes a list and two positions, swaps the elements at those positions, and returns the modified list. The script repeatedly swaps the elements at positions 2 and 5 in the predefined list [53, 21, 54, 34, 34], displays the original and swapped lists, and then asks the user if they want to try again. The loop continues until the user decides to stop.

Output:

how-to-swap-element-in-list-using-python-1.jpg


There you have it we successfully created How to Swap Element in List using Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

328,328

328,336

Top