• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Swap Two Values of Variables in Python

Swap Two Values of Variables in Python

February 25, 2022 Leave a Comment

With Python, we can easily swap two values between variables. The easiest way is to use tuple unpacking.

x = 2
y = 3

x, y = y, x

print(x)
print(y)

#Output:
3
2

You can also use a temporary variable to swap the values of two variables.

x = 2
y = 3

temp_var = x
x = y
y = temp_var

print(x)
print(y)

#Output:
3
2

When working with variables in Python, being able to change the values of variables easily is important.

One such change is swapping the values between two variables.

We can easily swap values of two variables in Python. To swap values you can use a temporary variable, or the easiest way is to swap values is with tuple unpacking.

Below is an example in Python of how to swap the values of two variables using tuple unpacking.

x = 2
y = 3

x, y = y, x

print(x)
print(y)

#Output:
3
2

Swap Values in Python with Temporary Variable

We can also swap values in Python with the use of a temporary variable.

In this method, we first store the first value in the temporary variable, set the first variable equal to the second variable, and then set the second variable equal to the temporary variable.

After swapping the values, we won’t be using the temporary variable, making this method less optimal than the method of tuple unpacking.

Below is how to swap two variables in Python with a temporary variable.

x = 2
y = 3

temp_var = x
x = y
y = temp_var

print(x)
print(y)

#Output:
3
2

How to Swap Two Items in a List Using Python

We can also swap items in a list using Python using the same tuple unpacking method of how to swap two variables.

Below is an example in Python of how to swap two items in a list.

def swapPositions(lst,position1, position2):
    lst[position1], lst[position2] = lst[position2], lst[position1]
    return lst

print(swapPositions([0,1,2,3,4,5],2,3))

#Output:
[0,1,3,2,4,5]

Hopefully this article has been useful for you to learn how to swap two values with Python.

Other Articles You'll Also Like:

  • 1.  Remove Every Nth Element from List in Python
  • 2.  Using Python to Sum Odd Numbers in List
  • 3.  Remove Duplicates from Sorted Array in Python
  • 4.  Date Format YYYYMMDD in Python
  • 5.  Count Spaces in String in Python
  • 6.  Python Get Number of Cores Using os cpu_count() Function
  • 7.  nunique pandas – Get Number of Unique Values in DataFrame
  • 8.  Format Numbers as Currency with Python
  • 9.  Using Selenium to Close Browser in Python
  • 10.  Using Python to Remove First Character from String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy